I have two arrays basically and we need them to compare by checking if the order of elements inner values are both higher on the same column, then count as 1.
I will explain more by showing example
First array
const arr1 = [[1, 3], [6, 5], [4, 2]]
Second array
const arr2 = [[1, 2], [4, 6], [3, 2]]
Based on the above two, we will check the three element of both,
first element - arr1 has [1, 3] and arr2 has [1, 2] and as you can see both of them are higher on second column and it should count.
second element - arr1 has [6, 5] and arr2 has [4, 6] - both are higher on different columns and should not be counted.
third element - arr1 has [4, 2] and arr2 has [3, 2] - both are higher on first column and should be counted as well
and the result of above arrays should be '2 found'.
So far I tried out this, but it outputs 5 instead of 2.
const arr1 = [[1, 3], [6, 5], [4, 2]]
const arr2 = [[1, 2], [4, 6], [3, 2]]
function compare(arr1, arr2) {
count = 0
arr1.forEach((e1) => arr2.forEach((e2) => {
if (e1[0] > e1[1] && e2[0] > e1[1]) {
count += 1
} else if (e1[1] > e1[0] && e2[1] > e1[0]) {
count += 1
}
})
)
return count
}
result = compare(arr1, arr2)
console.log(result)
This is the solution. You were running .forEach twice.
const arr1 = [
[1, 3],
[6, 5],
[4, 2]
];
const arr2 = [
[1, 2],
[4, 6],
[3, 2]
];
function compare(arr1, arr2) {
let counter = 0;
arr1.map((value, index) => {
let foo = arr1[index];
let bar = arr2[index];
if ((foo[0] > foo[1] && bar[0] > bar[1] && foo[0] != bar[1]) || (foo[0] < foo[1] && bar[0] < bar[1] && foo[0] != bar[1])) {
counter += 1;
}
})
return counter;
}
let c = compare(arr1, arr2)
console.log(c);
You could make a function that finds that index of the max index of an array. Then just use that to test in a reduce() to count the matches:
// get index of max value in array
const maxIndex = (arr) => arr.reduce((max, curr, i, self) => curr > self[max] ? i : max, 0)
const arr1 = [[1, 3], [6, 5], [4, 2]]
const arr2 = [[1, 2], [4, 6], [3, 2]]
let count = arr1.reduce((count, arr, i) =>
maxIndex(arr) == maxIndex(arr2[i]) // count if the index of the max is the same
? count+1
: count
, 0)
console.log(count)
A reducer is definitely the way to go.
Keeping along the lines of what you wrote, You just need to do one loop through your one array, and then output that reduction.
const arr1 = [[1, 3], [6, 5], [4, 2]]
const arr2 = [[1, 2], [4, 6], [3, 2]]
const compare = (arr1, arr2) => arr1.reduce((r,o,i) =>
((o[0] > o[1] && arr2[i][0] > arr2[i][1]) ||
(o[0] < o[1] && arr2[i][0] < arr2[i][1])) ? r+1 : r
,0);
result = compare(arr1, arr2)
console.log(result)
Related
This is the array: [[1, 2], [1, 3], [1, 4]]
This is how it looks like with a regular for loop:
let multiplication = 1;
for (let i = 0; i < l.length; i++) {
multiplication *= l[i][1];
}
How do I do the same thing with reduce instead?
Initiate value at the second parameter of reduce, and with the callback function, remember to return the accumulated value
Below solution could help you
const arr = [
[1, 2],
[1, 3],
[1, 4],
];
const res = arr.reduce((acc, el) => acc * el[1], 1);
console.log(res);
The .length property on an array will return the number of elements in the array. For example, the array below contains 2 elements:
[1, [2, 3]] // 2 elements, number 1 and array [2, 3]
Suppose we instead wanted to know the total number of non-nested items in the nested array. In the above case, [1, [2, 3]] contains 3 non-nested items, 1, 2 and 3.
Examples
getLength([1, [2, 3]]) ➞ 3
getLength([1, [2, [3, 4]]]) ➞ 4
getLength([1, [2, [3, [4, [5, 6]]]]]) ➞ 6
You can flatten the array using .flat(Infinity) and then get the length. Using .flat() with an argument of Infinity will concatenate all the elements from the nested array into the one outer array, allowing you to count the number of elements:
const getLength = arr => arr.flat(Infinity).length;
console.log(getLength([1, [2, 3]])) // ➞ 3
console.log(getLength([1, [2, [3, 4]]])) // ➞ 4
console.log(getLength([1, [2, [3, [4, [5, 6]]]]])) // ➞ 6
You can use reduce on each array it'll find like this :
function getLength(arr){
return arr.reduce(function fn(acc, item) {
if(Array.isArray(item)) return item.reduce(fn);
return acc + 1;
}, 0);
}
console.log(getLength([1, [2, 3]]))
console.log(getLength([1, [2, [3, 4]]]))
console.log(getLength([1, [2, [3, [4, [5, 6]]]]]))
Recursively count the elements that you don't recurse into:
function getLength(a) {
let count = 0;
for (const value of a) {
if (Array.isArray(value)) {
// Recurse
count += getLength(value);
} else {
// Count
++count;
}
}
return count;
}
Live Example:
function getLength(a) {
let count = 0;
for (const value of a) {
if (Array.isArray(value)) {
count += getLength(value);
} else {
++count;
}
}
return count;
}
console.log(getLength([1, [2, 3]]));
console.log(getLength([1, [2, [3, 4]]]));
console.log(getLength([1, [2, [3, [4, [5, 6]]]]]));
You could just add the lengths for nested array or one.
function getLength(array) {
let count = 0;
for (const item of array) count += !Array.isArray(item) || getLength(item);
return count;
}
console.log(getLength([1, [2, 3]]));
console.log(getLength([1, [2, [3, 4]]]));
console.log(getLength([1, [2, [3, [4, [5, 6]]]]]));
I have an array of arrays and I want to check if there is a tie between the second elements and then return the first element of the last array that makes a tie.
for example this should return 4. (the first element in the last array that has a second element that makes a tie)
var optionsArray = [[1, 10], [2, 10], [3, 10], [4, 10], [6, 14]];
It is quite simple, you need to iterate over your source array, check if the given item matches the criteria, and save it to result if it does. Now if any other item does match the criteria, result's value will be overwritten with the new matching item.
var optionsArray = [[1, 10], [2, 10], [3, 10], [4, 10], [6, 14]];
var result;
optionsArray.forEach(function(item) {
if(item[1] == 10) {
result = item;
}
});
console.log(result);
You can create a simple find function that iterates the array backwards, and returns as soon as a condition callback returns true.
var optionsArray = [[1, 10], [2, 10], [3, 10], [4, 10], [6, 14]];
function find10(s) {
return s[1] === 10;
}
function findFromTheEnd(arr, cb) {
var l = arr.length;
while(l--) { // iterate backwards
if(cb(arr[l])){ // if the callback returns true
return arr[l]; // return the item
}
}
return null; // return null if none found
}
var result = findFromTheEnd(optionsArray, find10);
console.log(result);
You can use reduceRight() and return array.
var arr = [[1, 10], [2, 10], [3, 10], [4, 10], [6, 14]];
var result = arr.reduceRight(function(r, e) {
if(e[1] == 10 && !r) r = e;
return r;
}, 0)
console.log(result)
You can also use for loop that starts from end and break on first match.
var arr = [[1, 10], [2, 10], [3, 10], [4, 10], [6, 14]];
var result;
for (var i = arr.length - 1; i >= 0; i--) {
if (arr[i][1] == 10) {
result = arr[i]
break;
}
}
console.log(result)
A classic for in the reserve order with a break seems enough :
var optionsArray = [[1, 10], [2, 10], [3, 10], [4, 10], [6, 14]];
var elementFound;
for (var i = optionsArray.length-1; i >=0; i--) {
if(optionsArray[i].item[1] == 10) {
elementFound = optionsArray[i].item[1];
break;
}
}
If elementFound is not undefined, it refers to the found array.
Rather than considering this as a multidimensional array problem, think of it as an array includes problem nested in an array search problem;
const aarr = [1, 2, 3, 4];
aarr.includes(3); // true
aarr.includes(10); // false
// and
const barr = ['hello', 'world'];
barr.find(item => item[0] === 'h'); // "hello"
barr.find(item => item[3] === 'l'); // "hello"
barr.find(item => item[1] === 'z'); // undefined
So to nest these,
const carr = [[1, 2, 3, 4], [4, 5, 6, 7]];
carr.find(arr => arr.includes(4)); // [1, 2, 3, 4]
carr.find(arr => arr.includes(6)); // [4, 5, 6, 7]
Next, we've reduced the whole problem down to "how to do this in reverse?"
You've a few options depending on how you want to implement it, but a simple way to do it is a shallow clone arr.slice() followed by a reverse arr.reverse() (we use the clone so there are no side-effects of reverse on the original array)
carr.slice().reverse().find(arr => arr.includes(4)); // [4, 5, 6, 7]
If you're working with an index, remember that you'll need to transform those too; -1 is fixed, otherwise transformed_index = arr.length - original_index - 1
Here is how you might implement the reverse of some of the Array methods
const optionsArray = [[1, 10], [2, 10], [3, 10], [4, 10], [6, 14]];
// index 0 1 2 3 4
function arrLast(arr, comparator, method = 'find', transform = x => x) {
return transform(arr.slice().reverse()[method](comparator), arr);
}
const findLast = (arr, comparator) => arrLast(arr, comparator);
const findLastIndex = (arr, comparator) => arrLast(arr, comparator, 'findIndex', (i, arr) => i === -1 ? -1 : arr.length - i - 1);
arrLast(optionsArray, arr => arr.includes(10)); // [4, 10]
findLastIndex(optionsArray, arr => arr.includes(10)); // 3
If you have to make comparisons among array items and you need to cut short once you are satisfied a while loop is ideal. Accordingly you may do as follows;
var arr = [[1, 10], [2, 10], [3, 10], [4, 10], [6, 14]],
i = 0,
result;
while (arr[i][1] === arr[++i][1]);
result = arr[i-1][0]
console.log(result);
I have two arrays playerMoves and movesList. Like this.
var playerMoves= [4, 6];
var movesList= [[0,1,2],[0,3,6]];
I need to filter the movesList array such that values of playerMoves array should not be present in each array of movesList.
console.log(move);
// should return [0,1,2]
My attempt
var playerMoves= [4, 6];
var movesList= [[0,1,2],[0,3,6]];
var move = movesList.filter(v => v.filter(c => {
return playerMoves.indexOf(c) === -1;
}));
console.log(move);
You can use mix of Array#filter, Array#every and Array#includes.
let playerMoves = [4, 6];
let movesList = [
[0, 1, 2],
[0, 3, 6],
[5, 7, 9],
];
let res = movesList.filter(v => v.every(c => !playerMoves.includes(c)));
console.log(JSON.stringify(res));
Let's say that I have an array of arrays, like so:
[
[0, 1, 3],
[2, 4, 6],
[5, 5, 7],
[10, 0, 3]
]
How do I generate a new array that sums all of the values at each position of the inner arrays in javascript? In this case, the result would be: [17, 10, 19]. I need to be able to have a solution that works regardless of the length of the inner arrays. I think that this is possible using some combination of map and for-of, or possibly reduce, but I can't quite wrap my head around it. I've searched but can't find any examples that quite match this one.
You can use Array.prototype.reduce() in combination with Array.prototype.forEach().
var array = [
[0, 1, 3],
[2, 4, 6],
[5, 5, 7],
[10, 0, 3]
],
result = array.reduce(function (r, a) {
a.forEach(function (b, i) {
r[i] = (r[i] || 0) + b;
});
return r;
}, []);
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
Update, a shorter approach by taking a map for reducing the array.
var array = [[0, 1, 3], [2, 4, 6], [5, 5, 7], [10, 0, 3]],
result = array.reduce((r, a) => a.map((b, i) => (r[i] || 0) + b), []);
console.log(result);
Using Lodash 4:
function sum_columns(data) {
return _.map(_.unzip(data), _.sum);
}
var result = sum_columns([
[1, 2],
[4, 8, 16],
[32]
]);
console.log(JSON.stringify(result));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
For older Lodash versions and some remarks
Lodash 4 has changed the way _.unzipWith works, now the iteratee gets all the values passed as spread arguments at once, so we cant use the reducer style _.add anymore. With Lodash 3 the following example works just fine:
function sum_columns(data) {
return _.unzipWith(data, _.add);
}
var result = sum_columns([
[1, 2],
[4, 8, 16],
[32],
]);
console.log(JSON.stringify(result));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>
_.unzipWith will insert undefineds where the row is shorter than the others, and _.sum treats undefined values as 0. (as of Lodash 3)
If your input data can contain undefined and null items, and you want to treat those as 0, you can use this:
function sum_columns_safe(data) {
return _.map(_.unzip(data), _.sum);
}
function sum_columns(data) {
return _.unzipWith(data, _.add);
}
console.log(sum_columns_safe([[undefined]])); // [0]
console.log(sum_columns([[undefined]])); // [undefined]
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>
This snipet works with Lodash 3, unfortunately I didn't find a nice way of treating undefined as 0 in Lodash 4, as now sum is changed so _.sum([undefined]) === undefined
One-liner in ES6, with map and reduce
var a = [ [0, 1, 3], [2, 4, 6], [5, 5, 7], [10, 0, 3] ];
var sum = a[0].map((_, i) => a.reduce((p, _, j) => p + a[j][i], 0));
document.write(sum);
Assuming that the nested arrays will always have the same lengths, concat and reduce can be used.
function totalIt (arr) {
var lng = arr[0].length;
return [].concat.apply([],arr) //flatten the array
.reduce( function(arr, val, ind){ //loop over and create a new array
var i = ind%lng; //get the column
arr[i] = (arr[i] || 0) + val; //update total for column
return arr; //return the updated array
}, []); //the new array used by reduce
}
var arr = [
[0, 1, 3],
[2, 4, 6],
[5, 5, 7],
[10, 0, 3]
];
console.log(totalIt(arr)); //[17, 10, 19]
Assuming array is static as op showned.
a = [
[0, 1, 3],
[2, 4, 6],
[5, 5, 7],
[10, 0, 3]
]
b = []
for(i = 0; i < a[0].length; i++){
count = 0
for(j = 0; j < a.length; j++){
count += a[j][i]
}
b.push(count)
}
console.log(b)
So far, no answer using the for ... of mentioned in the question.
I've used a conditional statement for different lengths of inner arrays.
var a = [
[0, 1, 3],
[2, 4, 6],
[5, 5, 7],
[10, 0, 3]
];
i = 0;
r = []
for (const inner of a) {
j = 0;
for (const num of inner) {
if (j == r.length) r.push(num)
else r[j] += num
j++;
}
i++;
}
console.log(r);
True, in this case, the classic for cycle fits better than for ... of.
The following snippet uses a conditional (ternary) operator.
var a = [
[0, 1, 3],
[2, 4, 6],
[5, 5, 7],
[10, 0, 3]
];
r = [];
for (i = 0; i < a.length; i++) {
for (j = 0; j < a[i].length; j++) {
j==r.length ? r.push(a[i][j]) : r[j]+=a[i][j]
}
}
console.log(r);
A solution using maps and reductions, adding elements from different lengths of arrays.
var array = [
[0],
[2, 4],
[5, 5, 7, 10, 20, 30],
[10, 0]
];
b = Array(array.reduce((a, b) => Math.max(a, b.length), 0)).fill(0);
result = array.reduce((r, a) => b.map((_, i) => (a[i] || 0) + (r[i] || 0)), []);
console.log(result);
const ar = [
[0, 1, 3],
[2, 4, 6],
[5, 5, 7],
[10, 0, 3]
]
ar.map( item => item.reduce( (memo, value)=> memo+= value, 0 ) )
//result-> [4, 12, 17, 13]