I would like to perform a logical OR operation on arrays in javascript, so that performing this operation on arrays a
and b
let a = [0,1,0]
let b = [0,0,5]
gives me
OR(a, b)
[0,1,5]
What would be an efficient and idiomatic way to implement OR
?
You could reduce the wanted arrays by taking a mapping and an or
function.
const or = (a, b) => a || b,
mapped = fn => (a, b) => a.map((v, i) => fn(v, b[i]));
var a = [0, 1, 0],
b = [0, 0, 5],
c = [a, b].reduce(mapped(or));
console.log(c);
Just use ||
operator in combination with map
method by passing a callback function as argument.
let a = [0,1,0]
let b = [0,0,5]
let c = a.map((item, index) => item || b[index]);
console.log(c);
I was wondering how I can make my canvas resize along with my browser window? As it is right now, using innerWidth and innerHeight, it will only size to the initial browser width and height. How do I ...
I was wondering how I can make my canvas resize along with my browser window? As it is right now, using innerWidth and innerHeight, it will only size to the initial browser width and height. How do I ...
Having ajax request like: $.ajax({ type: 'POST', url: 'mysite/action', dataType: 'json', data: postData, success: function(response) { console.log(response); var input = $('<a ...
Having ajax request like: $.ajax({ type: 'POST', url: 'mysite/action', dataType: 'json', data: postData, success: function(response) { console.log(response); var input = $('<a ...
I'm trying to loop through nested arrays to determine if an element in the array is either "open" or "senior": function openOrSenior(data) { for (let i = 0; i <= data.length; i++) { let ...
I'm trying to loop through nested arrays to determine if an element in the array is either "open" or "senior": function openOrSenior(data) { for (let i = 0; i <= data.length; i++) { let ...
I have this array air_content: '', compaction_method: 1, concrete_cylinders: [ { id: '', specimen_name: 'A', mould_number: '', curing: 1, age: 7 }, { ...
I have this array air_content: '', compaction_method: 1, concrete_cylinders: [ { id: '', specimen_name: 'A', mould_number: '', curing: 1, age: 7 }, { ...