gpt4 book ai didi

javascript - 比较两个包含对象的数组以计算发生了什么变化?

转载 作者:搜寻专家 更新时间:2023-11-01 05:28:46 25 4
gpt4 key购买 nike

使用平面 JavaScript 或通过使用 lodash,我比较以下数组并返回已更改的值的最简单方法是什么(希望 lodash 有一个函数):

之前

[
{id: 0, name: 'Bob', age: 27},
{id: 1, name: 'Frank', age: 32},
{id: 2, name: 'Joe', age: 38}
]

之后

[
{id: 0, name: 'Bob', age: 27},
{id: 1, name: 'Frank', age: 33},
{id: 2, name: 'Joe', age: 38}
]

所以在之前和之后之间,Frank 现在已经 33 岁了,那么我怎么能简单地返回:

{id: 1, name: 'Frank', age: 33}

或者更理想的结果:

{id: 1, age: 33}

编辑:

由于我的问题得到了如此多样的答案,我决定在服务器端和客户端测试它们。这是我使用 json-generator 后得到的结果生成包含 10 000 条记录的 json 文件:

节点 7.1.0:

David Domain. (Flat JS filter & some): 3.396
Result: { id: 1, name: 'Frank', age: 33 }
Ben Aston (Flat JS nested itteration): 4.359
Result: { age: 33, id: 1 }
Gille Q. (Lodash reduce): 21.335
Result: { id: 1, age: 33 }
Stasovlas. (Lodash differenceBy): 1.442
Result: []
Vignesh Murugan. (Lodash findWhere): 0
Result: _.findWhere is not a function

Firefox 50.0.2:

David Domain. (Flat JS filter & some): 6.695
Result: { id: 1, name: 'Frank', age: 33 }
Ben Aston (Flat JS nested itteration): 10.594
Result: { age: 33, id: 1 }
Gille Q. (Lodash reduce): 40.085
Result: { id: 1, age: 33 }
Stasovlas. (Lodash differenceBy): 6.499
Result: []

这里要注意的有趣的一点是,当您处理大量数据时,Lodash differenceBy 似乎不起作用,在我放弃之前,我充其量只能使用 3 条记录。

@Vignesh 一定在某个时候使用过 Underscore,但我不打算介绍这一点,因为情况已经发生变化,我们现在使用 Lodash。

这是我用来测试的代码,及时跟踪执行函数所花费的时间,然后循环 1000 次以获得执行函数 1000 次的总时间然后除以 1000 得到平均时间执行函数所花费的时间(以毫秒为单位):

var fs = require('fs');
var timely = require('timely');
var _ = require('lodash');

// Ben Aston
var ben_aston = function (a, b) {
return a.reduce((p,c,i)=>{
var diff = objDiff(c, b[i]);
diff && p.push(diff);
return p;
}, [])
}
function objDiff(a, b) {
var diff = Object.keys(a).reduce((p,c,i)=>{
if (a[c] === b[c]) {
return p;
}
p[c] = b[c];
return p;
}, {});
if (!Object.keys(diff).length) {
return;
}
diff.id = a.id;
return diff;
}
var ben_astonT = timely(ben_aston);


// Gille Q.
var gille_q = function (before, after) {
return _.reduce(before, function(result, value, key) {
return _.isEqual(value, after[key]) ?
result : result.concat({id: after[key].id, age: after[key].age});
}, []);
}
var gille_qT = timely(gille_q);


// David Domain
var david_domain = function (before, after) {
return after.filter( function( p, idx ) {
return Object.keys(p).some( function( prop ) {
return p[prop] !== before[idx][prop];
})
})
}
var david_domainT = timely(david_domain);


// Stasovlas
var stasovlas = function (before, after) {
return _.differenceBy(after, before, 'age');
}
var stasovlasT = timely(stasovlas);


// Vignesh Murugan
var vignesh_murugan = function (before, after) {
before.forEach((current) => {
var after = _.findWhere(after,{id : current.id});
if(!_.isEqual(after , current)) {
return _.pick(after,"id","name");
}
});
}
var vignesh_muruganT = timely(vignesh_murugan);


// Load the data
var before = JSON.parse(fs.readFileSync('./before.json', 'utf8'));
var after = JSON.parse(fs.readFileSync('./after.json', 'utf8'));

// Open average tracking
var ben_aston_ave = 0,
gille_q_ave = 0,
david_domain_ave = 0,
stasovlas_ave = 0,
vignesh_murugan_ave = 0;

// Do test
for (i = 0; i < 1000; i++) {
// Ben Aston
ben_astonT(before, after);
ben_aston_ave += ben_astonT.time;

// Gille Q.
gille_qT(before, after);
gille_q_ave += gille_qT.time;

// David Domain
david_domainT(before, after);
david_domain_ave += david_domainT.time;

// Stasovlas
stasovlasT(before, after);
stasovlas_ave += stasovlasT.time;

// Vignesh Murugan
// vignesh_muruganT(before, after);
// vignesh_murugan_ave += vignesh_muruganT.time;
}

// Calc averages
ben_aston_ave = ben_aston_ave / 1000;
gille_q_ave = gille_q_ave / 1000;
david_domain_ave = david_domain_ave / 1000;
stasovlas_ave = stasovlas_ave / 1000;
vignesh_murugan_ave = vignesh_murugan_ave / 1000;


console.log('David Domain. (Flat JS filter & some): '+david_domain_ave);
console.log('Result: { id: 1, name: \'Frank\', age: 33 }');
console.log('Ben Aston (Flat JS nested itteration): '+ben_aston_ave);
console.log('Result: { age: 33, id: 1 }');
console.log('Gille Q. (Lodash reduce): '+gille_q_ave);
console.log('Result: { id: 1, age: 33 }');
console.log('Stasovlas. (Lodash differenceBy): '+stasovlas_ave);
console.log('Result: []');
console.log('Vignesh Murugan. (Lodash findWhere): '+vignesh_murugan_ave);
console.log('Result: _.findWhere is not a function');

最佳答案

使用_.differenceBy

var res = _.differenceBy(after, before, 'age');

关于javascript - 比较两个包含对象的数组以计算发生了什么变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41122068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com