Javascript 如何使 JSON 数组唯一

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11826302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:15:03  来源:igfitidea点击:

How to make a JSON array unique

javascriptjqueryarraysjsondojo

提问by coolguy

Possible Duplicate:
Array unique values
Get unique results from JSON array using jQuery

可能的重复:
数组唯一值
使用 jQuery 从 JSON 数组中获取唯一结果

Im having a JSON string like this

我有一个像这样的 JSON 字符串

[
 Object { id="38",product="foo"},
 Object { id="38",product="foo"},
 Object { id="38",product="foo"},
 Object { id="39",product="bar"},
 Object { id="40",product="hello"},
 Object { id="40",product="hello"}

]

There are duplicate values in this JSON array..how can i make this JSON array unique like this

这个 JSON 数组中有重复的值..我怎样才能让这个 JSON 数组像这样独一无二

[
 Object { id="38",product="foo"},
 Object { id="39",product="bar"},
 Object { id="40",product="hello"}
]

.Im looking for a suggestion that uses less iterations, Jquery $.inArrayis not working in this case.

.我正在寻找使用较少迭代的建议, Jquery $.inArray在这种情况下不起作用。

Suggestion to use any third party libraries are welcome.

欢迎建议使用任何第三方库。

采纳答案by Flater

Check the solution in the following SO question:

检查以下 SO 问题中的解决方案:

Get unique results from JSON array using jQuery

使用 jQuery 从 JSON 数组中获取唯一结果

You'll have to iterate through your array and create a new array which contains unique values.

您必须遍历数组并创建一个包含唯一值的新数组。

回答by Laurent Perrin

You can use underscore's uniq.

您可以使用下划线的 uniq

In your case, you need to provide an iterator to extract 'id':

在您的情况下,您需要提供一个迭代器来提取“id”:

array = _.uniq(array, true /* array already sorted */, function(item) {
  return item.id;
});

回答by Aesthete

// Assuming first that you had **_valid json_**
myList= [
    { "id":"38","product":"foo"},
    { "id":"38","product":"foo"},
    { "id":"38","product":"foo"},
    { "id":"39","product":"bar"},
    { "id":"40","product":"hello"},
    { "id":"40","product":"hello"}
];

// What you're essentially attempting to do is turn this **list of objects** into a **dictionary**.
var newDict = {}

for(var i=0; i<myList.length; i++) {
    newDict[myList[i]['id']] = myList[i]['product'];
}

// `newDict` is now:
console.log(newDict);

回答by Torsten Walter

You can easily code this yourself. From the top of my head this comes to mind.

您可以轻松地自己编写代码。从我的头顶想到这一点。

var filtered = $.map(originalArray, function(item) {
    if (filtered.indexOf(item) <= 0) {
        return item;
    }
});

Or as suggested a more efficient algorithm specifically for the case at hand:

或者建议一种更有效的算法,专门针对手头的情况:

var helper = {};
var filtered = $.map(originalArray, function(val) {
    var id = val.id;

    if (!filtered[id]) {
        helper[id] = val;
        return val;
    }
});
helper = null;

回答by SReject

You will probably have to loop through removing the duplicates. If the items stored are in order as you have suggested, it's a simple matter of a single loop:

您可能需要遍历删除重复项。如果存储的项目按照您的建议按顺序排列,则只需一个循环即可:

function removeDuplicates(arrayIn) {
    var arrayOut = [];
    for (var a=0; a < arrayIn.length; a++) {
        if (arrayOut[arrayOut.length-1] != arrayIn[a]) {
            arrayOut.push(arrayIn[a]);
        }
    }
    return arrayOut;
}