简体   繁体   English

JS-在其中一个对象上添加新的属性和值

[英]JS - Add a new property and value on one of the objects

I have a list of objects 我有一个对象清单

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"]

It would be 这将是

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

Tried with map 尝试过地图

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

You could just assign the new property. 您可以只分配新属性。

With a classic assignment: 经典作业:

 const array = [{ club: "Club A", number: 12 }, { club: "Club B", number: 12 }], member = ["a", "b"]; array.find(({ club }) => club === "Club A").member = member; console.log(array); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Or take Object.assign 或采用Object.assign

 const array = [{ club: "Club A", number: 12 }, { club: "Club B", number: 12 }], member = ["a", "b"]; Object.assign(array.find(({ club }) => club === "Club A"), { member }); console.log(array); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

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) 

You could use Object.assign in map 您可以在地图中使用Object.assign

 const array = [ {club: "Club A", number: 12}, {club: "Club B", number: 12} ] const member = {member: ["a", "b"]}; const newArray = array.map(obj => obj.club === 'Club A' ? Object.assign(obj, member) : obj); console.log(newArray); 

You can use find() to get the item you want to edit. 您可以使用find()获得要编辑的项目。 Then just assign the property to it: 然后只需为其分配属性:

 const array = [ {club: "Club A", number: 12}, {club: "Club B", number: 12} ] // find the correct item let item = array.find(item => item.club == "Club A") item.member = ["a", "b"] console.log(array) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 JS 中将一个对象数组的属性值添加到另一个对象数组(用于匹配对象) - How to add property value from one array of objects into another (for the matching objects) in JS 是否可以在js中为对象的新属性添加值? - Is it possible to add value to a new property of an object in js? 查找具有相同属性值的对象并将它们添加到新的数组属性中 - Find objects with same property value and add them in new array property 将具有默认值的新属性添加到 javascript 对象数组 - Add new property with default value to javascript array of objects 如何在JS中使用特定参数/值向子对象添加属性? - How to add a property to sub-objects with a certain parameter/value in JS? 根据值将一个带有对象的数组中的属性添加到另一个数组 - Add property from one array with objects to another based on value 向对象数组添加新属性 - Add new property to an array of objects 根据现有对象的属性值创建新的对象数组 object 将现有对象添加到新数组 - Create a new array of objects by property value from existing object add existing objects to new array 如何在不具有指定属性的数组中添加某些对象的新属性? (JS) - How to add new property of some objects in the array that doesn't have the specified property? (JS) 将新值推送到对象数组,同时保持旧值不变 React Js Next Js - Pushing New value to Array of Objects while keeping old one intact React Js Next Js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM