I want to merge this array using or || operator
[[true,false,false],[false,false,false],[false,false,true]]
so that the output is
[true,false,true]
is this possible with map or reduce or similar?
Edit: Sorry for the unclear question - yes it was to vertically merge all sub arrays together. So the following input:
[[true,false,false],[false,false,false],[false,false,true],[false,false,true]]
would produce the same output:
[true,false,true]
I want to merge this array using or || operator
[[true,false,false],[false,false,false],[false,false,true]]
so that the output is
[true,false,true]
is this possible with map or reduce or similar?
Edit: Sorry for the unclear question - yes it was to vertically merge all sub arrays together. So the following input:
[[true,false,false],[false,false,false],[false,false,true],[false,false,true]]
would produce the same output:
[true,false,true]
Share
Improve this question
edited Sep 10, 2017 at 2:48
Soth
asked Sep 9, 2017 at 15:16
SothSoth
3,0452 gold badges30 silver badges29 bronze badges
4
- 1 What is the logical condition for merging? – brk Commented Sep 9, 2017 at 15:17
-
2
1. Do you want that first
true
to bea[0][0] || a[0][1] || a[0][2]
ora[0][0] || a[1][0] || a[2][0]
? 2. What have you tried to solve the problem? – T.J. Crowder Commented Sep 9, 2017 at 15:21 - 1 Well, thanks to the unclear question, you now have answers both ways. :-) marvel308's if you want the first one, Nina's if you want the second. – T.J. Crowder Commented Sep 9, 2017 at 15:21
- To put TJ's observation in another way, are you merging vertically or horizontally? Are you merging each array or are you merging value at an index across all arrays? – vol7ron Commented Sep 9, 2017 at 15:27
3 Answers
Reset to default 4You don't really need the ||
when you use some
:
var array = [[true, false, false], [false, false, false], [false, false, true]];
var result = array[0].map( (_, i) => array.some(a => a[i]));
console.log(result);
You could reduce the arrays with mapping the same index values.
var array = [[true, false, false], [false, false, false], [false, false, true]],
result = array.reduce((a, b) => a.map((c, i) => b[i] || c));
console.log(result);
you can do it in the following way
let arr = [[true,false,false],[false,false,false],[false,false,true]];
arr = arr.map(function(element){
return element.reduce(function(a, b){
return (a|b);
})
})
console.log(arr);