Array and javascript - how to compare two arrays -
is there way value changed when comparing 2 arrays?
i'll explain better.
let's have
arr1 = [1,2,3] arr2 = [1,2,3]
arr2
created dynamically, , default same arr1
when change arr2
arr2 = [0,2,3]
how can detect value changed? (in case, 1 changed 0)?
if want know value in index changed, , changed value, can use array#reduce create array of changes.
var arr1 = [1, 2, 3]; var arr2 = [0, 2, 3]; function whatchanged(arr1, arr2) { return arr2.reduce(function(d, v, i) { v !== arr1[i] && d.push({ index: i, from: arr1[i], to: v }); return d; }, []); } var diff = whatchanged(arr1, arr2); console.log(diff);
Comments
Post a Comment