I have two arrays of objects (Obj1
and Obj2
).
The difference is that Obj2
objects have an extra property called fn. I want to compare both arrays and if an object in Obj1
has fn in Obj2
for the same datakey
then want to add fn
in Obj1 also (the datakey
is unique).
I don’t want to change the order of Obj1
array and I don’t want to remove any extra object from Obj1
.
I tried the following but, it doesn’t seem to work or I am doing the wrong way.
JavaScript
x
64
64
1
let Obj1 = [
2
{
3
"dataKey": "aaa",
4
"title": "Lorem ipsum",
5
"description": "Dolor sit",
6
"flag": true
7
},
8
{
9
"dataKey": "ccc",
10
"title": "dsff fsfsfs",
11
"description": "dsd ds ds ds",
12
"flag": false
13
},
14
{
15
"dataKey": "bbb",
16
"title": "Duis aute irure",
17
"description": "eu fugiat nulla pariatur",
18
"flag": false
19
},
20
{
21
"dataKey": "ddd",
22
"title": "Lorem ipsum dsds",
23
"description": "Dolor sit dsdsds",
24
"flag": true
25
},
26
];
27
28
let Obj2 = [
29
{
30
"dataKey": "aaa",
31
"title": "Lorem ipsum",
32
"description": "Dolor sit",
33
"flag": true,
34
"fn": function() {
35
console.log('hi');
36
}
37
},
38
{
39
"dataKey": "bbb",
40
"title": "Duis aute irure",
41
"description": "eu fugiat nulla pariatur",
42
"flag": true
43
},
44
{
45
"dataKey": "ccc",
46
"title": "dsff fsfsfs",
47
"description": "dsd ds ds ds",
48
"flag": true,
49
"fn": function() {
50
console.log('hi');
51
return 'abcd';
52
}
53
},
54
];
55
56
Obj1.forEach(function(item){
57
Obj2.forEach(function(newitem) {
58
if(item.dataKey === newitem.dataKey && newitem.fn) {
59
item["fn"] = newitem.fn;
60
}
61
})
62
})
63
64
console.log(Obj1);
Expected Output:
JavaScript
1
34
34
1
let Obj1 = [
2
{
3
"dataKey": "aaa",
4
"title": "Lorem ipsum",
5
"description": "Dolor sit",
6
"flag": true,
7
"fn": function() {
8
console.log('hi');
9
}
10
},
11
{
12
"dataKey": "ccc",
13
"title": "dsff fsfsfs",
14
"description": "dsd ds ds ds",
15
"flag": false,
16
"fn": function() {
17
console.log('hi');
18
return 'abcd';
19
}
20
},
21
{
22
"dataKey": "bbb",
23
"title": "Duis aute irure",
24
"description": "eu fugiat nulla pariatur",
25
"flag": false
26
},
27
{
28
"dataKey": "ddd",
29
"title": "Lorem ipsum dsds",
30
"description": "Dolor sit dsdsds",
31
"flag": true
32
},
33
];
34
Advertisement
Answer
Just iterate Obj2
and use Array.find()
to find the corresponding value on Obj1
JavaScript
1
62
62
1
let Obj1 = [
2
{
3
"dataKey": "aaa",
4
"title": "Lorem ipsum",
5
"description": "Dolor sit",
6
"flag": true
7
},
8
{
9
"dataKey": "ccc",
10
"title": "dsff fsfsfs",
11
"description": "dsd ds ds ds",
12
"flag": false
13
},
14
{
15
"dataKey": "bbb",
16
"title": "Duis aute irure",
17
"description": "eu fugiat nulla pariatur",
18
"flag": false
19
},
20
{
21
"dataKey": "ddd",
22
"title": "Lorem ipsum dsds",
23
"description": "Dolor sit dsdsds",
24
"flag": true
25
},
26
];
27
28
let Obj2 = [
29
{
30
"dataKey": "aaa",
31
"title": "Lorem ipsum",
32
"description": "Dolor sit",
33
"flag": true,
34
"fn": function() {
35
console.log('hi');
36
}
37
},
38
{
39
"dataKey": "bbb",
40
"title": "Duis aute irure",
41
"description": "eu fugiat nulla pariatur",
42
"flag": true
43
},
44
{
45
"dataKey": "ccc",
46
"title": "dsff fsfsfs",
47
"description": "dsd ds ds ds",
48
"flag": true,
49
"fn": function() {
50
console.log('hi');
51
return 'abcd';
52
}
53
},
54
];
55
56
Obj2.forEach(function(newitem) {
57
const obj = Obj1.find(item => item.dataKey === newitem.dataKey);
58
if (newitem.fn)
59
obj.fn = newitem.fn;
60
})
61
62
console.log(Obj1);