Since I don't know how this works very well and other questions such as:
How to compare arrays in JavaScript?
or, JavaScript array difference
are completely different questions and after 1 and a half hours of scouring the web no results I am going to go and ask this question simply. Say you have two arrays [1,2,3]
and [1,4,1]
. Then how would you get a program to make our variable (say x) to be [1,2]
(because column 1 and 2 are different if you count the first column as column 0)?
Asked
Active
Viewed 49 times
1

Community
- 1
- 1
3 Answers
1
Please see this:
var array1 = [1, 2, 3];
var array2 = [1, 4, 1];
var columndiff = [];
for (i = 0; i < array1.length; i++) {
if (array1[i] != array2[i])
columndiff.push(i);
}
console.log(columndiff);
Note: Here we are assuming that array1
and array2
has equal length

vijayP
- 11,432
- 5
- 25
- 40
-
Thanks so much dude! – Aug 08 '16 at 08:57
0
ES2015 code:
const a = [1, 2, 3];
const b = [1, 4, 1];
const result = a.reduce((res, val, index) => {
if (val !== b[index]) res.push(index);
return res;
}, []);
console.log(result); // [1, 2]

Maxx
- 1,740
- 10
- 17
0
You can simply do like this;
var arr = [1,3,4,5,2,3],
brr = [1,3,5,5,1,2],
res = arr.reduce((p,c,i) => c !== brr[i] ? p.concat(i) : p,[]);
console.log(res);

Redu
- 25,060
- 6
- 56
- 76