Reputation: 385
I have a list of object below
the newlist is also dynamic and SelectID array is also dynamic
I am populating thorugh a function , now I have to iteratte and create the new list
var newList =
[
{ id : 1,name="tea",plant:"darjeeling"},
{ id : 2,name="coffee",plant:"manipur"},
{ id : 3,name="milk",plant:"nilgiri"},
{ id : 4,name="tea",plant:"assam"}
]
anaother array which has ID in common
var selectedID = [2,3];
now I have to iterate over the list of objects and update the list of objects as wherever the ID is 2 and 3 plant should be "munnar "
so how to create new list of objects "newlist" like below
var newList =
[
{ id : 1,name="tea",plant:"darjeeling"},
{ id : 2,name="coffee",plant:"munnar"},
{ id : 3,name="milk",plant:"munnar"},
{ id : 4,name="tea",plant:"assam"}
]
Upvotes: 0
Views: 81
Reputation: 26
I'm assuming that you are doing this in javascript, so first I want to point out you should have newList defined like this
var newList =
[
{ id : 1,name:"tea",plant:"darjeeling"},
{ id : 2,name:"coffee",plant:"manipur"},
{ id : 3,name:"milk",plant:"nilgiri"},
{ id : 4,name:"tea",plant:"assam"}
]
name="tea" won't work.
But onto your problem, you'll want to use the selectedID array values to update your existing newList like so.
var newList =
[
{ id : 1,name:"tea",plant:"darjeeling"},
{ id : 2,name:"coffee",plant:"manipur"},
{ id : 3,name:"milk",plant:"nilgiri"},
{ id : 4,name:"tea",plant:"assam"}
]
var selectedID =[2,3]
for(i=0;i<newList.length;i++){
for(j=0;j < selectedID.length;j++){
if(newList[i].id==selectedID[j])
newList[i].plant = "munnar";
}
}
console.log(newList);
Upvotes: 1
Reputation: 644
From what I understand you wish to manipulate the original array with different values based on an array of selected ids. If those selected ID's which could come from the UI match any of the ids in your array of plants then change the name to Munnar? If I have understood correctly then I believe the following should work for you
var newList = [{
id: 1,
name: "tea",
plant: "darjeeling"
}, {
id: 2,
name: "coffee",
plant: "manipur"
}, {
id: 3,
name: "milk",
plant: "nilgiri"
}, {
id: 4,
name: "tea",
plant: "assam"
}]
function munnar(selectedID) {
newList.forEach( (item) => {
selectedID.forEach( (id) => {
if(item.id === id) {
item.plant = 'Munnar'
}
})
})
}
munnar([2, 3])
This returns
[{
id: 1,
name: "tea",
plant: "darjeeling"
}, {
id: 2,
name: "coffee",
plant: "Munnar"
}, {
id: 3,
name: "milk",
plant: "Munnar"
}, {
id: 4,
name: "tea",
plant: "assam"
}]
Upvotes: 0
Reputation: 386624
You could iterate the wanted id for changing, find the object and change the value.
Using:
and a default object {}
, if no object is found.
var newList = [{ id: 1, name: "tea", plant: "darjeeling" }, { id: 2, name: "coffee", plant: "manipur" }, { id: 3, name: "milk", plant: "nilgiri" }, { id: 4, name: "tea", plant: "assam" }],
selectedID = [2, 3];
selectedID.forEach(id => (newList.find(a => a.id === id) || {}).plant = 'munnar');
console.log(newList);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 1