admin管理员组文章数量:1394985
Is there a better way to iterate over two array of objects than what I have done below? It seems messy to do it this way. I'm using lodash.
var array1 = [
{id:4356, name: 'James', sex: 'male'},
{id:7899, name: 'Jimmy', sex: 'male'},
{id:2389, name: 'Dawn', sex: 'female'}
];
var array2 = [
{id:4356, salary: 1000, job: 'programmer'},
{id:7899, salary: 2000, job: 'tester'},
{id:2389, salary: 3000, job: 'manager'}
];
Example output:
console.log(array1[0])
{
id:4356,
name: James,
sex: male,
person: {
id:4356,
salary: 1000,
job: programmer
}
}
Function:
_.forEach(array1, function(item1) {
_.forEach(array2, function(item2) {
if(item1.id === item2.id){
item1.person = item2;
}
});
});
Is there a better way to iterate over two array of objects than what I have done below? It seems messy to do it this way. I'm using lodash.
var array1 = [
{id:4356, name: 'James', sex: 'male'},
{id:7899, name: 'Jimmy', sex: 'male'},
{id:2389, name: 'Dawn', sex: 'female'}
];
var array2 = [
{id:4356, salary: 1000, job: 'programmer'},
{id:7899, salary: 2000, job: 'tester'},
{id:2389, salary: 3000, job: 'manager'}
];
Example output:
console.log(array1[0])
{
id:4356,
name: James,
sex: male,
person: {
id:4356,
salary: 1000,
job: programmer
}
}
Function:
_.forEach(array1, function(item1) {
_.forEach(array2, function(item2) {
if(item1.id === item2.id){
item1.person = item2;
}
});
});
Share
Improve this question
edited Jan 15, 2016 at 13:05
Adam Boduch
11.2k3 gold badges32 silver badges38 bronze badges
asked Jan 14, 2016 at 19:39
tester123tester123
1,1232 gold badges12 silver badges18 bronze badges
3
-
Convert one of the arrays into an
id -> person
map first? – Felix Kling Commented Jan 14, 2016 at 19:42 - Possible duplicate of Functional way of joining two js object arrays based on mon id – nograde Commented Jan 14, 2016 at 19:57
- Gruff Bunny's answer (linked above) is pretty prehensive. – nograde Commented Jan 14, 2016 at 19:59
2 Answers
Reset to default 6Since you're using lodash, you could use the _.find()
method to find the corresponding object in array2
based on the id
properties.
_.forEach(array1, function(item1) {
item1.person = _.find(array2, {id: item1.id});
});
It's worth pointing out that this will result in an undefined person
property if an object isn't found. If that's a problem, simply check to see if an object is returned:
_.forEach(array1, function(item1) {
var obj = _.find(array2, {id: item1.id});
if (obj) {
item1.person = obj;
}
});
Without lodash, it would be pretty similar:
array1.forEach(function(item1) {
item1.person = array2.find(function (item2) {
return item2.id === item1.id;
});
});
I would build a reference object and add to it, that way you're not loading the second array N times. Something like this:
var array1 = [{id:4356, name: 'James', sex: 'male'},
{id:7899, name: 'Jimmy', sex: 'male'},
{id:2389, name: 'Dawn', sex: 'female'}
];
var array2 = [{id:4356, salary: 1000, job: 'programmer'},
{id:7899, salary: 2000, job: 'tester'},
{id:2389, salary: 3000, job: 'manager'}
];
var array3 = {};
for(var i in array1) {
array3[array1[i]['id']] = array1[i];
}
for(var i in array2) {
for(var key in array2[i]) {
array3[array2[i]['id']][key] = array2[i][key];
}
}
This gives you an object with the properties of both arrays like:
Object {2389: Object, 4356: Object, 7899: Object}
With the benefit that you only iterate over each array once.
本文标签: arraysNested forEach loops to add object to existing object javascriptStack Overflow
版权声明:本文标题:arrays - Nested forEach loops to add object to existing object javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744106925a2591105.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论