Home > Software design >  Replaceing object with array
Replaceing object with array

Time:02-15

I have two arrays as follows:

First array is as follows-

array1 = [{
 "id":1,
 "cityDetails": {55:1,56:20},
 "name": "sebastien"
},
{
 "id":2,
 "cityDetails": {55:1,56:20},
 "name": "Lora"
}
]

Second array is as follows -

array2 = [
{
 "id":1,
 "cityDetails": [
 {
     "cityId":55.
     "cityname":"london",
     "code":1
 },
  {
     "cityId":56.
     "cityname":"paris",
     "code":1
 }
 ],
 "name": "sebastien"    
},
{
 "id":2,
  "cityDetails": [
 {
     "cityId":55.
     "cityname":"london",
     "code":1
 },
  {
     "cityId":56.
     "cityname":"paris",
     "code":1
 }
 ],
 "name": "Lora"
}
]

array two can have more items. I want to replace cityDetails object in array1 with cityDetails array of array2 on the basis of id. How can I do that?

Also Just want to replace object cityDetails in array1 with cityDetails array (which is a array in second array) for every cityId. If in cityDetails array cityId not found then add new element to array with name and cityId. if for particular element cityId matches, then replace value of "name" with right hand side value of object for that cityId. Note- "name" value need to replaced with right hand side value of object for that id and cityId

CodePudding user response:

Have a try with this.

array1.map(item => {
    const index = array2.findIndex(arr2Item => arr2Item.id === item.id);
    if (index !== -1) {
        item = {
          ...item,
            cityDetails: {
                ...(array2[index].cityDetails || []),
            },
        };
    }
    return item;
})

CodePudding user response:

Using Map in first array, find Index from second array with matching id and Update.

let array1 = [{
    "id": 1,
    "cityDetails": { 55: 1, 56: 20 },
    "name": "sebastien"
},
{
    "id": 2,
    "cityDetails": { 55: 1, 56: 20 },
    "name": "Lora"
}
]

let array2 = [
    {
        "id": 1,
        "cityDetails": [
            {
                "cityId": 55,
                "cityname": "london",
                "code": 1
            },
            {
                "cityId": 56,
                "cityname": "paris",
                "code": 1
            }
        ],
        "name": "sebastien2"
    },
    {
        "id": 2,
        "cityDetails": [
            {
                "cityId": 55,
                "cityname": "london",
                "code": 1
            },
            {
                "cityId": 56,
                "cityname": "paris",
                "code": 1
            }
        ],
        "name": "Lora2"
    }
]

array1.map(a1 => {
    a1.cityDetails = [];
    let index = array2.findIndex(a2=> a2.id == a1.id);
    if (index > -1){
        a1.cityDetails = array2[index].cityDetails;
    }
});
console.log(array1);

CodePudding user response:

try this:

function replaceCityDetails(array1,array2){
        array1.forEach(item1=>{
            array2.forEach(item2=>{
                if(item1.id == item2.id){
                    item1['cityDetails'] = item2['cityDetails'];
                }
            });
        });
    }
    
    replaceCityDetails(array1, array2);

CodePudding user response:

When you create objects, do not put keys in DOUBLE QUOTES.

The next code:

array1 = [{
 "id":1,
 "cityDetails": {55:1,56:20},
 "name": "sebastien"
},
{
 "id":2,
 "cityDetails": {55:1,56:20},
 "name": "Lora"
}
]

Should be like this:

array1 = [{
 id:1,
 cityDetails: {55:1,56:20},
 name: "sebastien"
},
{
 id:2,
 cityDetails: {55:1,56:20},
 name: "Lora"
}
]

To solve your problem, answer my question: Do you want to replace cityDetail in array1 with the values of cityDail of array2 if the id of object in array1 equals to id of an object in array2?

  • Related