admin管理员组文章数量:1327971
I have a multidimensional array that looks like this:
var workouts = [
[0, Object, 0, 0, 0],
[Object, 0, 0, 0, 0],
[0, 0, 0, Object, 0]
];
I'd like to flatten/merge the array and remove the duplicates. The result should look something like this:
[Object, Object, 0, Object, 0]
Is it possible to perform?
I have a multidimensional array that looks like this:
var workouts = [
[0, Object, 0, 0, 0],
[Object, 0, 0, 0, 0],
[0, 0, 0, Object, 0]
];
I'd like to flatten/merge the array and remove the duplicates. The result should look something like this:
[Object, Object, 0, Object, 0]
Is it possible to perform?
Share Improve this question asked Aug 11, 2016 at 10:47 malenmalen 951 silver badge4 bronze badges 4- 3 How are you defining removing duplicates? Why is 0 in your result twice, is that not a duplicate? What have you tried? – UnknownFury Commented Aug 11, 2016 at 10:49
- only 2 zeroes are there in your expected Output.. Is that right? It supposed to have 3 zeroes isn't it? – Rajaprabhu Aravindasamy Commented Aug 11, 2016 at 10:51
- Yes, it should be 2 zeros in the expected output. My plan is to loop the expected output and then render different elements depending on the 0 / the object. – malen Commented Aug 11, 2016 at 10:55
- Are you mapping the indices of inner arrays and mark the ones with the object if an inner array has an onbect at that index or 0 if not? – Redu Commented Aug 11, 2016 at 11:00
4 Answers
Reset to default 2My understanding is that you want to keep the first encountered object in a given column if it exists.
You can do that with .map()
and .reduce()
:
var workouts = [
[0, {id:1}, 0, 0, 0],
[{id:2}, 0, 0, 0, 0],
[0, 0, 0, {id:3}, 0]
];
var res = workouts.reduce(function(a, b) {
return b.map(function(e, i) { return a[i] instanceof Object ? a[i] : e; });
}, []);
console.log(JSON.stringify(res));
Late for the party, but I still want to make use of:
Array.prototype.flat()
for reducing the dimension to 1,Set
for removing dupesSpread Syntax
for getting array out of that set.JSON.stringify
and parse back for handling dupes of Objects.
Here's the code and a very inspirational posts 1 and 2:
const myArray = [
[0, {id:1}, 0, 0, 0],
[{id:2}, 0, 0, 0, 0],
[0, 0, 0, {id:2}, 0]
]
const myFlatArray = myArray.flat()
const result = [...new Set(myFlatArray.map(JSON.stringify))].map(JSON.parse)
console.log(JSON.stringify(result))
Note: ES6 must be supported for this solution
var workouts = [
[0, {id:1}, 0, 0, 0],
[{id:2}, 0, 0, 0, 0],
[0, 0, 0, {id:3}, 0]
];
function flattenArray (array){
var newArray = [];
for (i=0; i<array.length; i++){
var subArray = array[i];
for (ii=0; ii<subArray.length; ii++){
if (newArray.indexOf(subArray[ii]) == -1){
newArray.push(subArray[ii]);
}
}
}
return newArray;
}
console.log(flattenArray(workouts));
http://codepen.io/anon/pen/WxLrqL
I would do like this;
var workouts = [[0, {a:1}, 0, 0, 0],
[{b:2}, 0, 0, 0, 0],
[0, 0, 0, {c:3}, 0]
],
result = workouts.reduce((p,c) => p = c.map((e,i) => e || p[i] || 0 ),[])
console.log(result);
本文标签: Mergeflatten multidimensional array and remove duplicates JavascriptStack Overflow
版权声明:本文标题:Mergeflatten multidimensional array and remove duplicates Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742252829a2441080.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论