What is the best way to replace a string in an array, with an array from another array based on their matching values? I have been grinding my teeth for nearly a week on this, but no luck so far..
Say we have these 3 arrays in JavaScript:
// Array with list of content:
[
[
{id: "1-AAA", content: "foo"},
{id: "1-AAA", content: "foo"},
{id: "1-AAA", content: "bar"},
{id: "1-AAA", content: "baz"},
{id: "1-AAA", content: "baz"}
],
[
{id: "1-CCC", content: "bar"}
],
[
{id: "1-DDD", content: "foo"}
],
[
{id: "2-AAA", content: "foo"},
{id: "2-AAA", content: "bar"},
{id: "2-AAA", content: "bar"}
]
]
// Array 1 with unique id's and last known content:
[
{id: "1-AAA", content: "baz"},
{id: "1-BBB", content: undefined},
{id: "1-CCC", content: "bar"},
{id: "1-DDD", content: "foo"},
{id: "1-EEE", content: undefined}
]
// Array 2 with unique id's and last known content:
[
{id: "2-AAA", content: "bar"},
{id: "2-BBB", content: undefined},
{id: "2-CCC", content: undefined},
{id: "2-DDD", content: undefined},
{id: "2-EEE", content: undefined}
]
How can I assign the content values from the first array as an array of content to the specific id's (see code snippets below)? Preferably it should only have the unique values (f.e. by using [...new Set(content)]).
// Intended result for array 1:
[
{
id: "1-AAA",
content: ["foo", "bar", "baz"]
},
{
id: "1-CCC",
content: ["bar"]
},
{
id: "1-DDD",
content: ["foo"]
}
]
// Intended result for array 2:
[
{
id: "2-AAA",
content: ["foo", "bar"]
}
]
I tried many different things so far, but can't find a good solution for reassigning the nested arrays.. Any help would be much appreciated! Thanks in advance guys!