admin管理员组文章数量:1026989
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]: .png
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/1MvXz.png
Share Improve this question edited Jul 21, 2021 at 19:30 double-beep 5,53719 gold badges40 silver badges49 bronze badges asked Mar 15, 2016 at 14:46 Arek KostrzebaArek Kostrzeba 5791 gold badge8 silver badges22 bronze badges 2- 2 please add the array as text and add you code – Nina Scholz Commented Mar 15, 2016 at 14:47
- 1 What specifically are you having problems with? The general approach is pretty simple: iterate over the array and only add the values to the output array which you haven't seen yet. – Felix Kling Commented Mar 15, 2016 at 14:55
2 Answers
Reset to default 4You 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);
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)
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]: .png
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/1MvXz.png
Share Improve this question edited Jul 21, 2021 at 19:30 double-beep 5,53719 gold badges40 silver badges49 bronze badges asked Mar 15, 2016 at 14:46 Arek KostrzebaArek Kostrzeba 5791 gold badge8 silver badges22 bronze badges 2- 2 please add the array as text and add you code – Nina Scholz Commented Mar 15, 2016 at 14:47
- 1 What specifically are you having problems with? The general approach is pretty simple: iterate over the array and only add the values to the output array which you haven't seen yet. – Felix Kling Commented Mar 15, 2016 at 14:55
2 Answers
Reset to default 4You 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);
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)
本文标签: Remove duplicates of array from another arrayJavaScriptStack Overflow
版权声明:本文标题:Remove duplicates of array from another array, JavaScript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745299483a2144407.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论