0

As I dig deeper on javascript data structures. I am stuck on figuring out how to add properties to objects inside arrays. This is my code below

const reviews = [
    {name: "Daniela", rating: 5, feedback:"Beautiful atmosphere and wonderful vegan options!"},
    {name: "Jack", rating: 3, feedback:"A little too hipster for my taste, but the burger was decent, if overpriced"},
    {name: "Miranda", rating: 4, feedback:"fun trivia and cool vibes"},
    {name: "Wen", rating: 4.5, feedback:"I don't leave my house often, but when I do, it's for this place. Highly reccomend."},
    {name: "Brett", rating: 3, feedback: "great selection of snacks and a nice cafe area to get work done during the day."},
    {name: "Julius", rating: 2, feedback: "I was largely unimpressed by this venue. Nothing special on the menu and too expensive. The atmosphere is polarizing, and not for me, but I think some would like it." },
    {name: "Lauren", rating: 4, feedback: "Absolutely love that they have karaoke Fridays! Food and drink selection is okay."},
    {name: "Reyna", rating: 3.5, feedback: ""},
]

what I am trying to do here is to re-assign the feedback property for name: "Reyna

I have tried something like

reviews.push({
    name: 'Reyna',
    rating: 3.5,
    feedback: 'This place was okay'
  }
)

I am really stuck and trying so hard to figure out what it is that I am doing wrong

Emma
  • 27,428
  • 11
  • 44
  • 69
CleverOscar
  • 115
  • 1
  • 2
  • 9
  • 1
    Does this answer your question? [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – Heretic Monkey Nov 18 '20 at 21:35

2 Answers2

0

You will first need to find your object and then you can modify it.

const reynaReview = reviews.find(review => review.name === 'Reyna');
reynaReview.feedback = 'New Feedback';
ChrisG
  • 2,637
  • 18
  • 20
0

There are several ways you can do this.

You don't have to push the new object to the array, but you want to update the existing object.

So, the first task is to find the object. To do so, You can use Array.find, or Lodash library to find, or if you know the element index, then you can directly access it like array[index].

const reviews = [
    {name: "Daniela", rating: 5, feedback:"Beautiful atmosphere and wonderful vegan options!"},
    {name: "Jack", rating: 3, feedback:"A little too hipster for my taste, but the burger was decent, if overpriced"},
    {name: "Miranda", rating: 4, feedback:"fun trivia and cool vibes"},
    {name: "Wen", rating: 4.5, feedback:"I don't leave my house often, but when I do, it's for this place. Highly reccomend."},
    {name: "Brett", rating: 3, feedback: "great selection of snacks and a nice cafe area to get work done during the day."},
    {name: "Julius", rating: 2, feedback: "I was largely unimpressed by this venue. Nothing special on the menu and too expensive. The atmosphere is polarizing, and not for me, but I think some would like it." },
    {name: "Lauren", rating: 4, feedback: "Absolutely love that they have karaoke Fridays! Food and drink selection is okay."},
    {name: "Reyna", rating: 3.5, feedback: ""},
]

const myObj = reviews[7];
console.log('array[index]: ', myObj);

const myObj2 = reviews.find(x => x.name == "Reyna");
console.log('Array.find: ', myObj2);

myObj.feedback = 'whatever';

console.log(reviews);
Rinkesh Golwala
  • 979
  • 1
  • 7
  • 17