简体   繁体   中英

How to convert 2 object into single one

i've got 2 objects

const size = shoppingCart.map((el) => ({size: el.size}));

The first one is filteredProducts list:

    const filteredProducts = [
    {
        description: "Material: 100% cotton\nCare instructions: Do not tumble dry, machine wash at 30 ° C, machine wash on gentle cycle",
        id: "80005397",
        image: {url: 'h,ttps://www.datocms-assets.com/59095/1638273249-spodnie.png'},
        price: 32.8,
        title: " Pier One Cargo"
    }
]

And second one that is selected size:

  const selectedSize = [
    {
        size: 'L'
    }
]

I have to combine this 2 objects into single one like this:

    const filteredProducts = [
    {
        description: "Material: 100% cotton\nCare instructions: Do not tumble dry, machine wash at 30 ° C, machine wash on gentle cycle",
        id: "80005397",
        image: {url: 'h,ttps://www.datocms-assets.com/59095/1638273249-spodnie.png'},
        price: 32.8,
        title: " Pier One Cargo",
        size: 'L'
    }
]

You can use map() function and the spread operator :

const filteredProductsWithSize = filteredProducts.map((product, i) => ({
    ...product,
    ...selectedSize[i]
}));

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