Arek Kostrzeba
Arek Kostrzeba

Reputation: 579

Remove duplicates of array from another array, JavaScript

How can i remove duplicated arrays in this data structure?

[![enter image description here][1]][1]

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. [1]: https://i.sstatic.net/1MvXz.png

Upvotes: 1

Views: 5470

Answers (2)

David Vicente
David Vicente

Reputation: 3111

In this other reply it was resolved stringifying the arrays, and removing duplicates with a Set. It should be much simpler

Array.from(new Set(jsonData.map(JSON.stringify)), JSON.parse)

Upvotes: 1

baao
baao

Reputation: 73221

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);

Upvotes: 4

Related Questions