JS-在其中一个对象上添加新的属性和值 [英] JS - Add a new property and value on one of the objects

查看:414
本文介绍了JS-在其中一个对象上添加新的属性和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象列表

const array = [
    {club: "Club A", number: 12},
    {club: "Club B", number: 12}
]

我想在其中一个对象中添加新属性

I want to add a new property in one of the objects

member: ["a", "b"]

应该是

const newArray = [
    {club: "Club A", number: 12, member: ["a", "b"]},
    {club: "Club B", number: 12}
]

尝试过地图

const newArray = array.map(obj => check if obj.club ==="Club A" )

推荐答案

您可以使用 对象分解

You can use map and object destrcturing

const array = [{club: "Club A", number: 12},{club: "Club B", number: 12}]
let member = ["a", "b"]

const newArray = array.map(obj => obj.club === "Club A" ? {...obj,member} : obj)

console.log(newArray)

这篇关于JS-在其中一个对象上添加新的属性和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆