Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  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'
    }
]

>Solution :

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

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading