javascript - 访问数组对象属性
问题描述
当我深入研究 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'
}
)
我真的被困住了,努力想弄清楚我做错了什么
解决方案
您首先需要找到您的对象,然后您可以修改它。
const reynaReview = reviews.find(review => review.name === 'Reyna');
reynaReview.feedback = 'New Feedback';
推荐阅读
- java - URL 更改编码 + 而不是 %20
- java - 根据已颁发许可证的条件颁发许可证的信号量
- python - 从 Python 2 升级到 Python 3 Google App Engine
- javascript - webpack之后如何访问我的javascript?
- r - 如何使用ggplot中的数字变量排序的分组分类变量生成子图?
- kubernetes - 如何使用 go-template 获得与 kubectl get pod output 相同的输出
- javascript - 在 jquery 中使用 array() 设置 cookie
- asp.net-mvc - 将当前模型传递给部分视图,处理从服务器到客户端的大数据
- c# - 登录成功后传递用户详细信息来自身份asp.net core 3.1
- javascript - 使用 oninput="validity.valid||(value=' ');" 时如何使输入值恢复到原始值 而不是空的输入槽