首页 > 解决方案 > 访问数组对象属性

问题描述

当我深入研究 javascript 数据结构时。我一直在弄清楚如何向数组内的对象添加属性。这是我下面的代码

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

我在这里要做的是重新分配feedback财产name: "Reyna

我尝试过类似的东西

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

我真的被困住了,努力想弄清楚我做错了什么

标签: javascriptarraysobject

解决方案


您首先需要找到您的对象,然后您可以修改它。

const reynaReview = reviews.find(review => review.name === 'Reyna');
reynaReview.feedback = 'New Feedback';

推荐阅读