0

I am trying to reproduce my original problem; I have two arrays in react state Array 1 is an original array from Database and Array2 is an updated array is state.

Objective is to update only changed rates and not quantity (and other pararmters) back to the Database, hence i need to update the values of rates in object of Array1 with the values of the rates in object 2 for a the objects of Array1 matching with the objects in Array2.

Array1 = [{
    id: 1,
    name: IceCream,
    details: [{ id: "12", name: "milk", quantity: "50", rate: "100" },
    { id: "13", name: "cream", quantity: "50", rate: "300" }]
},
{
    id: 2,
    name: Coffee,
    details: [{ id: "14", name: "Coffee bean", quantity: "60", rate: "200" },
    { id: "15", name: "water", quantity: "60", rate: "300" }]

},
{
    id: 3,
    name: Tea,
    details: [{ id: "16", name: "Tea leaf", quantity: "50", rate: "700" }]
}]


Array2 = [{
    id: 1,
    name: IceCream,
    details: [{ id: "12", name: "milk", quantity: "50", rate: "500" },
    { id: "13", name: "cream", quantity: "50", rate: "700" }]
},
{
    id: 2,
    name: Coffee,
    details: [{ id: "14", name: "Coffee bean", quantity: "60", rate: "800" },
    { id: "15", name: "water", quantity: "60", rate: "8000" }]
}]
3
  • Don't ask the same question twice? Commented Aug 23, 2020 at 19:35
  • My Apologies Sir..I was not able to reproduce problem correctly in my previous attempt.. Commented Aug 23, 2020 at 19:37
  • Fastest way would be to have extra hashmaps of each column's value as key and value as id for faster search (more like caching). Otherwise you will have toloop through smaller array, and then loop through bigger array and find the matches manually using if conditions. Commented Aug 23, 2020 at 19:39

1 Answer 1

2

You need to iterate over the objects in Array1 using .map, check if it exists in Array2 by id using .find. Then, iterate over the details and update the rate if it also exists in that of the second array:

let Array1 = [
{
     id:1,
     name: "IceCream",
     details:[
          {id:"12",name:"milk",quantity:"50",rate:"100"}, 
          {id:"13",name:"cream",quantity:"50",rate:"300"}
     ]
},
{
     id:2,
     name:"Coffee",
     details:[
          {id:"14",name:"Coffee bean",quantity:"60",rate:"200"},
          {id:"15",name:"water",quantity:"60",rate:"300"}
     ]
},
{ 
     id:3,
     name:"Tea",
     details:[
          {id:"16",name:"Tea leaf",quantity:"50",rate:"700"}
     ]
}
]

let Array2 = [
{
     id:1,
     name: "IceCream",
     details:[
          {id:"12",name:"milk",quantity:"50",rate:"500"}, 
          {id:"13",name:"cream",quantity:"50",rate:"700"}
     ]
},
{
     id:2,
     name:"Coffee",
     details:[
          {id:"14",name:"Coffee bean",quantity:"60",rate:"800"},
          {id:"15",name:"water",quantity:"60",rate:"8000"}
     ]     
}
]

Array1 = Array1.map(item => {
     let element = Array2.find(e => e.id == item.id);
     if(element){
          item.details = item.details.map(e => {
               let detail = element.details.find(d => d.id==e.id);
               if(detail)
                    e.rate = detail.rate;
               return e;
          });
     }
     return item;
});

console.log(Array1);

Sign up to request clarification or add additional context in comments.

5 Comments

HI Majed Thank you for you time.When I put this code in my application I am getting " Cannot assign to read only property 'rate' of object '#<Object>'"
let r = [...state.basicRecipe]; let Array2 = [...state.recipeBasicRecipes]; r = r.map(item => { let element = Array2.find(e => e._id == item._id); if (element) { item.details = item.details.map(e => { let detail = element.details.find(d => d._id == e._id); if (detail) e.rate = detail.rate; return e; }); } return item; }); console.log(r);
These arrays are part of the react state.
Oh, I cannot help a lot here. But I think there should be a special method to update the state. Research more about the problem using React.