简体   繁体   中英

how to embed an object in another one

I have

let inside = { description: 'some text'};

what I need to do is first create a new object from the above

make

enhanced = { description: 'some text',
                  fielda: 'a',
                  fieldb: 'b;
                }

to accomplish the above I use the spread operator ...

now what is left to do is transform enhanced to have an outer key such as

enhancedOuter = 
    { item: 
         description: 'some text',
         fielda: 'a',
         fieldb: 'b
    }

any thoughts on how to do what is left without using keys and values and hand crafting the final object?

您正在寻找的是

 { item: enhanced }

Just create a key with desired name and put the value,

 let inside = { description: 'some text' }; let field = { fielda: 'a', fieldb: 'b' } let enhanced = { item : {...inside,...field} } console.log(enhanced) 

(Note: This won't work on MS Edge)

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