Home > Software design >  Combining Javascript objects
Combining Javascript objects

Time:10-09

I am creating a food menu app using reactjs at the frontend and django backend. I unfortunately got stuck here mostly because of my little knowledge of Javascript.Here is my problem. I have this array of objects

const objectOfArray = [
    {
    "id":"1a2f2c7a-0e06-4784-a1fb-68e083a72aa5","cat_id":"812347b3-615f-4f4a-b201-64737ddf4718","event_id":"ead9898c-9606-49e0-aa2b-c7f34331a856"
    },
    {
    "id":"7bee12ac-7253-4e5a-a86f-2ea0a3765541","cat_id":"6968da9d-4475-4431-801d-e3eeda02145a","event_id":"ead9898c-9606-49e0-aa2b-c7f34331a856"
    },
    {
    "id":"42763c0d-84f1-4f07-97c8-aa914bfb92fa","cat_id":"812347b3-615f-4f4a-b201-64737ddf4718","event_id":"ead9898c-9606-49e0-aa2b-c7f34331a856"
    }
]

then another object const myObject = {"seat":"4","table":"10"}

I want each of the object in objectOfArray to have myObject so that the output now looks like this

[
    {
    "id":"1a2f2c7a-0e06-4784-a1fb-68e083a72aa5","cat_id":"812347b3-615f-4f4a-b201-64737ddf4718","event_id":"ead9898c-9606-49e0-aa2b-c7f34331a856","seat":"4","table":"10"
    },
    {
    "id":"7bee12ac-7253-4e5a-a86f-2ea0a3765541","cat_id":"6968da9d-4475-4431-801d-e3eeda02145a","event_id":"ead9898c-9606-49e0-aa2b-c7f34331a856","seat":"4","table":"10"
    },
    {
    "id":"42763c0d-84f1-4f07-97c8-aa914bfb92fa","cat_id":"812347b3-615f-4f4a-b201-64737ddf4718","event_id":"ead9898c-9606-49e0-aa2b-c7f34331a856","seat":"4","table":"10"
    }
]

CodePudding user response:

As Lawrence suggested in a comment you can accomplish this using map with spread syntax:

const objectOfArray = [
    {
    "id":"1a2f2c7a-0e06-4784-a1fb-68e083a72aa5","cat_id":"812347b3-615f-4f4a-b201-64737ddf4718","event_id":"ead9898c-9606-49e0-aa2b-c7f34331a856"
    },
    {
    "id":"7bee12ac-7253-4e5a-a86f-2ea0a3765541","cat_id":"6968da9d-4475-4431-801d-e3eeda02145a","event_id":"ead9898c-9606-49e0-aa2b-c7f34331a856"
    },
    {
    "id":"42763c0d-84f1-4f07-97c8-aa914bfb92fa","cat_id":"812347b3-615f-4f4a-b201-64737ddf4718","event_id":"ead9898c-9606-49e0-aa2b-c7f34331a856"
    }
]

const myObject = {"seat":"4","table":"10"};

const result = objectOfArray.map(item => ({...item, ...myObject}));

console.log(result);

CodePudding user response:

You can assign the properties in myObject to some other object with Object.assign().

You also need a way to iterate over the original array: .map will do that.

Combining these two, you get

const objectOfArray = [
        {
            "id":"1a2f2c7a-0e06-4784-a1fb-68e083a72aa5","cat_id":"812347b3-615f-4f4a-b201-64737ddf4718","event_id":"ead9898c-9606-49e0-aa2b-c7f34331a856"
        },
        {
            "id":"7bee12ac-7253-4e5a-a86f-2ea0a3765541","cat_id":"6968da9d-4475-4431-801d-e3eeda02145a","event_id":"ead9898c-9606-49e0-aa2b-c7f34331a856"
        },
        {
            "id":"42763c0d-84f1-4f07-97c8-aa914bfb92fa","cat_id":"812347b3-615f-4f4a-b201-64737ddf4718","event_id":"ead9898c-9606-49e0-aa2b-c7f34331a856"
        }
    ];

const myObject = {"seat":"4","table":"10"}

newArray = objectOfArray.map(e=>Object.assign(e,myObject));

console.log(newArray);

  • Related