Remove duplicates of array from another array, JavaScript

柔情痞子 提交于 2019-12-25 05:13:34

问题


How can i remove duplicated arrays in this data structure?

I got this:

    ["5", "26", 300],
    ["7", "10", 20],
    ["3", "4", 30],
    ["5", "2", 52],
    ["9", "5", 300],
    ["3", "4", 30],
    ["5", "2", 52],
    ["5", "26", 300],
    ["1", "27", 250]

with:

var all = [].concat(jsonData['l'],jsonData['c'], jsonData['r']);                                    
for (e in all){
    console.log([all[e].source, all[e].target, Number(all[e].link)]);
}

I need to reduce data, remove duplicated arrays and provide result to sankey graf. jsonData elements contain much more data and structure of each left, center and right side is a little bit diffrent.

Resolved: I have build my own function based on mmm idea.

function dedupe(all) {
   var seen = [];
   var res = [];
   for (e in all){
       var temp = [all[e].source, all[e].target, Number(all[e].link)];
       if (seen.indexOf(temp.toString()) < 0) {
           seen.push(temp.toString());
           res.push(temp);
       }
   }
   return res;
}

Thans.


回答1:


You could filter them:

var a = [[1, 2, 3], [1, 2, 3], [4, 5, 6], [4, 5, 6], ['foo']];
var tmp = [];

var b = a.filter(function (v) {
    if (tmp.indexOf(v.toString()) < 0) {
        tmp.push(v.toString());
        return v;
    }
});

console.log(b);


来源:https://stackoverflow.com/questions/36014530/remove-duplicates-of-array-from-another-array-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!