1

Maybe this is stupid but my state is an array of nested objects :o It looks as follows:

  const data = [
        {
            randomid1: {
                name: 'lorem',
                latinName: 'ipsem',
            },
        },
        {
            randomid2: {
                name: 'lorem2',
                latinName: 'ipsem2',
            },
        },
    ]

I need to update this array with a new name and/or latinName for one of the items (let's say randomid1.name .

I have been struggling with spreading etc but keep getting errors. Now im trying mapping over something but this too won't work. Anyone?

2
  • I don't know if this is an option for you, but if possible I would move the ID into the object with the name and latinName. The flatter the structure the easier it is to work with. Especially since as-is, the value of the ID is the object key, so you can't do quite as simple of a comparison as item.id == 1. Commented Nov 10, 2021 at 16:14
  • @BrianThompson I would love that but the way i create the nested object comes from this question I previously asked: stackoverflow.com/questions/69785811/… I see i asked for a nested object but I would indeed love for the id to be part of the other items. Don't think thats possible however when it comes from Firestore .. Commented Nov 10, 2021 at 16:30

3 Answers 3

1

You can check if the array contains a certain item using Array#find and Object#hasOwnProperty:

const 
  data = [
    { randomid1: { name: 'lorem', latinName: 'ipsem' } },
    { randomid2: { name: 'lorem2', latinName: 'ipsem2' } }
  ],
  id = 'randomid2';

const elem = data.find(e => e.hasOwnProperty(id));
if(elem) {
  elem[id].name = 'new name';
}

console.log(data);

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

1 Comment

I dont understand it yet but it works! Which is great. Am going to do some more research, thanks a lot!
1

I would do this :

// will iterate on all the top level objects
for (const itemWrapper of data) {
  // will iterate just once
  for (const item of Object.values(itemWrapper)) {
    if (item.name = 'whatever') {
      // update item
    }
  }
}

You can move this function and return when the item has been found for better performance.

PS : the data structure seems a bit hard to use, if you can change it to something that's indexed by what you're searching on, that would be great.

1 Comment

thanks thats really helpful! i will def look into making the data structure easier cause its giving me a headache all throughout the code
1

I think that the solution already provided is better but if you would like to know how it could be possible with map, here is one way doing it :

const data = [
  { randomid1: { name: 'lorem', latinName: 'ipsem' } },
  { randomid2: { name: 'lorem2', latinName: 'ipsem2' } }
];

const id = 'randomid2';

const newdata2 = data.map((elt) => {
  if (elt[id]) {
    return {[id] : {...elt[id], name: "new Name"}};
  }
 return elt;
});

console.log(newdata2)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.