0

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"}
]
3
  • 3
    and where is your special problem? Commented Apr 24, 2017 at 13:48
  • 3
    op has not asked any question. Commented Apr 24, 2017 at 13:53
  • 2
    @Kinduser, rereaded, no question found o_O Commented Apr 24, 2017 at 13:56

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

that would alter the newList based on index and not based on the id value.
cant alter the newlist , just have to change the values wherever its find the IDs
Here I fixed it... I can't stand that I did that wrong
1

You could iterate the wanted id for changing, find the object and change the value.

Using:

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; }

3 Comments

dont know why its not working in visual studio js file
@tripathy, it is ES6 style. but the better question is how many data do you have in newList and how many indices do you have in selectedID? how often does the content change? are the indices sorted?
indices wont be sorted they are just values coming from another function . SeletedID can containg many and also newlist can contain many object . but SelectedID will contain less or equal to the number of objects in list thats for sure
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.