[英]Add all objects with the same id from one array into an array of an object with the same id in another array
I have 2 arrays. I need to move all the objects that have the same id in arraySecondary into an array inside an object with the same id in arrayPrimary.我有 2 个 arrays。我需要将 arraySecondary 中具有相同 id 的所有对象移动到 arrayPrimary 中具有相同 id 的 object 中的数组中。
Example:例子:
const arrayPrimary = [
{ "id": "1", "location": "France", "price": "12,3" },
{ "id": "2", "location": "Germany", "price": "12,0" },
{ "id": "3", "location": "USA", "price": "10" },
{ "id": "4", "location": "Italy", "price": "16" },
];
const arraySecondary = [
{ "id": "1", "name": "phil", "location": "New York", "price": "1,3", "dd": "lql" },
{ "id": "2", "location": "Paris", "dd": "lql" },
{ "id": "3", "location": "Egypt" },
{ "id": "2", "name": "joe", "location": "London" },
{ "id": "1", "location": "location", "name": "april" },
{ "id": "2", "name": "mei", "location": "Barcelona" },
];
Expected result:预期结果:
[
{
id: 1,
location: "France",
price: "12,3",
area: [
{ id: 1, location: "location", name: "april" },
{ id: 1, name: "phil", location: "New York", price: "1,3", dd: "lql" },
],
},
{
id: 2,
location: "Germany",
price: "12,0",
area: [
{ id: 2, location: "Paris", dd: "lql" },
{ id: 2, name: "joe", location: "London" },
{ id: 2, name: "mei", location: "Barcelona" },
],
},
{ id: 3, location: "USA", price: 10, area: [{ id: 3, location: "Egypt" }] },
{ id: 4, location: "Italy", price: 16 },
];
//or json
First i add an empty array to each object in arrayPrimary.首先,我向 arrayPrimary 中的每个 object 添加一个空数组。
arrayPrimary.map((v) => ({ ...v, area: [] }));
After that i filter arraySecondary by id and push all the results into area array in each object in arrayPrimary.之后,我通过 id 过滤 arraySecondary,并将所有结果推送到 arrayPrimary 中每个 object 的区域数组中。 But here i get stuck.
但是在这里我被卡住了。
console.log(
arrayPrimary.forEach((main) =>
main.area.push(arraySecondary.filter((items) => items.id === main.id))
)
);
Second idea is to first order each object in arraySecondary by id and then push that into empty area array in arrayPrimary第二个想法是首先按 id 对 arraySecondary 中的每个 object 进行排序,然后将其推入 arrayPrimary 中的空区域数组
let op = arrayItems.reduce((op,inp) => {
op[inp.id] = op[inp.id] || []
op[inp.id].push(inp)
return op
},{})
console.log(op)
And this is where i am stuck with both ideas.这就是我坚持这两个想法的地方。
something like this?是这样的吗?
const arrayPrimary = [ { "id": "1", "location": "France", "price": "12,3" }, { "id": "2", "location": "Germany", "price": "12,0" }, { "id": "3", "location": "USA", "price": "10" }, { "id": "4", "location": "Italy", "price": "16" }, ]; const arraySecondary = [ { "id": "1", "name": "phil", "location": "New York", "price": "1,3", "dd": "lql" }, { "id": "2", "location": "Paris", "dd": "lql" }, { "id": "3", "location": "Egypt" }, { "id": "2", "name": "joe", "location": "London" }, { "id": "1", "location": "location", "name": "april" }, { "id": "2", "name": "mei", "location": "Barcelona" }, ]; const composed = arrayPrimary.map(d => { return {...d, area: arraySecondary.filter(({id}) => d.id === id) } }) console.log(composed)
You can do it as followed:您可以按如下方式进行:
const arrayResut=arrayPrimary.map(primaryElement=>{
primaryElement.area=arraySecondary.filter(secondaryElement=>secondaryElement.id==primaryElement.id);
return primaryElement;
})
This will create a copy of arrayPrimary
, and in the process add an area
key which will contain all the elements of arraySecondary
if the id of the arraySecondary
element is equal to the id of the arrayPrimary
element.这将创建
arrayPrimary
的副本,并在此过程中添加一个area
键,如果 arraySecondary 元素的 id 等于arraySecondary
元素的 id,它将包含arraySecondary
的arrayPrimary
元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.