Is there a way to get the value that changed when comparing two arrays?
有没有办法在比较两个数组时获得更改的值?
I'll explain it better.
我会更好地解释它。
let's say I have
让我说我有
arr1 = [1,2,3]
arr2 = [1,2,3]
arr2
get created dynamically, and the default is the same as arr1
arr2动态创建,默认值与arr1相同
When I change arr2
to arr2 = [0,2,3]
how can I detect the value that changed? (in this case, 1 changed to 0)?
当我将arr2更改为arr2 = [0,2,3]时,如何检测更改的值? (在这种情况下,1改为0)?
2 个解决方案
#1
1
If you just want to know which value in which index changed, and what is the changed value, you can use Array#reduce to create an array of changes.
如果您只想知道哪个索引更改了哪个值,以及更改的值是什么,则可以使用Array#reduce创建一个更改数组。
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);
#2
1
This is a simple code snippet that you can use like this:
这是一个简单的代码片段,您可以像这样使用:
1) First wrap your array with ArrayObserver
1)首先使用ArrayObserver包装数组
2) Then simply use the update
method to assign new array value
2)然后只需使用update方法分配新的数组值
3) Keep track of the new elements in onUpdated
callback
3)跟踪onUpdated回调中的新元素
4) Access your array at aObserver.array
4)在aObserver.array访问您的数组
Let me know if the code makes sense to you and if we need to change something
让我知道代码是否对您有意义,以及我们是否需要更改某些内容
var array = [10, 15];
var aObserver = new ArrayObserver(array);
aObserver.onUpdated(function(newValues, oldArray){
console.log('New values are:');
console.log(newValues);
});
/* use the update method to assing new value of the array */
aObserver.update([10, 12]);
aObserver.update([15, 12]);
console.log('array values is');
console.log(aObserver.array);
/*
Simple class that is holding the array and when array is updated through
this class it will give you the new values in a callback of onUpdated method
*/
function ArrayObserver(array){
var that = this;
var callback = null;
that.array = array;
that.update = function(newArray){
var newValues = [];
/* get a copy of the old array */
var oldValues = that.array.slice();
for(var i = 0; i < newArray.length; i++){
if(!~that.array.indexOf(newArray[i])){
/* this is a new value */
newValues.push(newArray[i]);
}
}
if(callback && {}.toString.call(callback) === '[object Function]'){
callback(newValues, oldValues);
}
that.array = newArray;
}
that.onUpdated = function(cb){
callback = cb;
}
}
#1
1
If you just want to know which value in which index changed, and what is the changed value, you can use Array#reduce to create an array of changes.
如果您只想知道哪个索引更改了哪个值,以及更改的值是什么,则可以使用Array#reduce创建一个更改数组。
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);
#2
1
This is a simple code snippet that you can use like this:
这是一个简单的代码片段,您可以像这样使用:
1) First wrap your array with ArrayObserver
1)首先使用ArrayObserver包装数组
2) Then simply use the update
method to assign new array value
2)然后只需使用update方法分配新的数组值
3) Keep track of the new elements in onUpdated
callback
3)跟踪onUpdated回调中的新元素
4) Access your array at aObserver.array
4)在aObserver.array访问您的数组
Let me know if the code makes sense to you and if we need to change something
让我知道代码是否对您有意义,以及我们是否需要更改某些内容
var array = [10, 15];
var aObserver = new ArrayObserver(array);
aObserver.onUpdated(function(newValues, oldArray){
console.log('New values are:');
console.log(newValues);
});
/* use the update method to assing new value of the array */
aObserver.update([10, 12]);
aObserver.update([15, 12]);
console.log('array values is');
console.log(aObserver.array);
/*
Simple class that is holding the array and when array is updated through
this class it will give you the new values in a callback of onUpdated method
*/
function ArrayObserver(array){
var that = this;
var callback = null;
that.array = array;
that.update = function(newArray){
var newValues = [];
/* get a copy of the old array */
var oldValues = that.array.slice();
for(var i = 0; i < newArray.length; i++){
if(!~that.array.indexOf(newArray[i])){
/* this is a new value */
newValues.push(newArray[i]);
}
}
if(callback && {}.toString.call(callback) === '[object Function]'){
callback(newValues, oldValues);
}
that.array = newArray;
}
that.onUpdated = function(cb){
callback = cb;
}
}