简体   繁体   English

如何将父 object 的 id 推送到子对象?

[英]How to push id of parent object to child objects?

I have an array of object which has an inner array of object, I want to push the id of parent object to each child object.我有一个 object 数组,它有一个 object 的内部数组,我想将父 object 的 id 推送给每个子 ZA8CFDE6331BD5B6666

   a = [
        {id: 'abc', stage: [{name: 'car' , value: '123'},{name: 'bus' , value: '345'},{name: 'truck' , value: '567'}],
        {id: 'def', stage: [{name: 'bike' , value: '890'},{name: 'cycle' , value: '123'},{name: 'car' , value: '456'}]}
    ]

expected output = [
    {name: 'car' , value: '123', id: 'abc'},{name: 'bus' , value: '345', 'abc'},{name: 'truck' , value: '567', 'abc'}, {name: 'bike' , value: '890', id: 'def'},{name: 'cycle' , value: '123',id: 'def',id: 'def'},{name: 'car' , value: '456', id: 'def'}
]

Im able to get the only the stage but not able to push id to each object.我能够获得唯一的舞台,但无法将 id 推送到每个 object。 pls help请帮忙

const getAllStages = [].concat(...map(a, el => el.stage));
console.log(getAllStages )

Use .map() again to add el.id to each element of el.stage .再次使用.map()el.stage添加到el.id的每个元素。

You can use .flatMap() in the outer mapping to concatenate all the results into a single array.您可以在外部映射中使用.flatMap()将所有结果连接到一个数组中。

 const a = [ {id: 'abc', stage: [{name: 'car', value: '123'},{name: 'bus', value: '345'},{name: 'truck', value: '567'}]}, {id: 'def', stage: [{name: 'bike', value: '890'},{name: 'cycle', value: '123'},{name: 'car', value: '456'}]} ] result = a.flatMap(({ id, stage }) => stage.map(s => ({ id: id, ...s }))); console.log(result);

You can write like this without using libraries您可以在不使用库的情况下这样编写

let a = [
    {
        id: 'abc', stage:
            [
                { name: 'car', value: '123' },
                { name: 'bus', value: '345' },
                { name: 'truck', value: '567' }
            ]
    },
    {
        id: 'def', stage:
            [
                { name: 'bike', value: '890' },
                { name: 'cycle', value: '123' },
                { name: 'car', value: '456' }
            ]
    }
]    
let output = [];
    for (const element of a) {
        for (const stageElement of element.stage) {
            let newElement = {
                name: stageElement.name,
                value: stageElement.value,
                id: element.id
            };
            output.push(newElement);
        }
    }

If you're trying to merge in id :如果您尝试合并id

let remapped = a.map(e => ({ id: a.id, ...e }));

Where that converts each entry into an object inheriting the a.id value and adding on whatever else is in the object.其中将每个条目转换为 object 继承a.id值并添加 object 中的其他任何内容。

Here is a example and result:)这是一个例子和结果:)

a.forEach(function(row) {
    row.stage.map(function (child) {child.id = row.id})
})

result:结果:

[{"id":"abc","stage":[{"name":"car","value":"123","id":"abc"},{"name":"bus","value":"345","id":"abc"},{"name":"truck","value":"567","id":"abc"}]},{"id":"def","stage":[{"name":"bike","value":"890","id":"def"},{"name":"cycle","value":"123","id":"def"},{"name":"car","value":"456","id":"def"}]}]
const a = [
    {id: 'abc', stage: [{name: 'car' , value: '123'},{name: 'bus' , value: '345'},{name: 'truck' , value: '567'}]},
    {id: 'def', stage: [{name: 'bike' , value: '890'},{name: 'cycle' , value: '123'},{name: 'car' , value: '456'}]}
]

a.forEach(item => {
    const itemId = item.id;
    const stages = item.stage;

    stages.forEach(stage => {
        stage['id'] = itemId;
    })
});

console.log(a);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用父对象中的 ID 引用将主对象与子对象合并到每个子对象 - How to merge Primary object with child objects using and ID reference in Parent to each child 如何将计算值推送到多维JSON对象并对父对象和子对象进行排序Vue.JS - How to Push Calculated Value to Multidimensional JSON Object and Sort Parent and Child Objects Vue.JS 将子JSON对象移动到具有父ID的自己的对象 - Move child JSON objects to their own object with parent ID 如何将数组推送到仅匹配父属性的嵌套子对象? - How to push array to nested child object of only matching parent properties? 将对象的子数组推入父数组 - Push child array of objects into parent array 如何通过ID删除子对象并使用过滤器检索父对象 - How to remove child by id and retrive the parent objects using filter 如何从子子对象获取父对象 - How to get parent object from child sub objects 使用lodash将子属性推入父对象 - Push child property to parent object using lodash 在JS Object中按ID查找一个孩子并将其推送 - Find a child by id in JS Object and push to it 从ID中获取与对象子数组与NodeJS中的猫鼬匹配的父对象 - Get parent object from id that matches objects child array with mongoose in NodeJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM