I have the following array if objects:
JavaScript
x
22
22
1
[{id: 0
2
name: "Weight"
3
options: [
4
"250gr","500gr"],
5
position: 0
6
variation: true
7
visible: true},
8
{id: 0
9
name: "Roast"
10
options: ["Light", "Medium, "Dark],
11
position: 0
12
variation: true
13
visible: true},
14
{id: 0
15
name: "Packaging"
16
options: [
17
"Tin","Card"],
18
position: 0
19
variation: true
20
visible: true}
21
]
22
Then i elaborate the following one:
JavaScript
1
8
1
[{id: 0, name: "Weight", option: "250gr"},
2
{id: 0, name: "Roast", option: "Medium"},
3
{id: 0, name: "Packaging", option: "Card"},
4
{id: 0, name: "Weight", option: "250gr"},
5
{id: 0, name: "Roast", option: "Light"},
6
{id: 0, name: "Packaging", option: "Card"}
7
]
8
is it possible to change the value of ‘options’ array of the first one, according to the second set of options? i need to obtain something like:
JavaScript
1
22
22
1
[{id: 0
2
name: "Weight"
3
options: [
4
"250gr"],
5
position: 0
6
variation: true
7
visible: true},
8
{id: 0
9
name: "Roast"
10
options: ["Light", "Medium"],
11
position: 0
12
variation: true
13
visible: true},
14
{id: 0
15
name: "Packaging"
16
options: [
17
"Card"],
18
position: 0
19
variation: true
20
visible: true}
21
]
22
Advertisement
Answer
i solved this way (locattributes is the first object, newopt the second) i don’t know if there is a more efficient way to do i
JavaScript
1
13
13
1
for (const property in locattributes) {
2
let locop = [];
3
locattributes[property].options = [];
4
newopt.forEach((no) => {
5
if (no.name === locattributes[property].name) {
6
if (locop.indexOf(no.option) === -1) {
7
locop.push(no.option);
8
}
9
}
10
});
11
locattributes[property].options = locop;
12
}
13