简体   繁体   中英

How to add parent key value to all child items in js array?

I have a json where one parent array, and every object of parent array there is another child array pozzles .

So what I want to do is, there is one key-value( title ) of parent item I want to add in every item of child array.

Check this json screen shot to understand clear.

在此处输入图像描述

Is there any direct method available in js to do this?

Considering this is your data

const data = [{ id: 1, title: "some", pozzles: [{ id: "1A" }, { id: "1B" }] }, { id: 2, title: "some2", pozzles: [{ id: "2A" }, { id: "2B" }] }];

data.forEach(d => {
    d.pozzles.forEach(p => { p.title = d.title })
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM