简体   繁体   English

如何为 JavaScript 中数组内的对象添加值?

[英]How to add value to an object which is inside an array in JavaScript?

I have an array with multiple object into it,我有一个包含多个对象的数组,

const tokens = 
      [ { to: "abc", sound: "default" } 
      , { to: "def", sound: "ring"    } 
      , { to: "ghi", sound: "vibrate" } 
      ] 

Further i want to add 2 different value say title and body to each object something like this :-此外,我想为每个对象添加 2 个不同的值,例如标题和正文,如下所示:-

Note:- I have more than 300-400 objects in array "tokens" so please try to reply with an efficient code注意:- 我在数组“tokens”中有超过 300-400 个对象,所以请尝试用有效的代码回复

const tokens = 
      [ { to: "abc", sound: "default", title: "test", body: "test2" } 
      , { to: "def", sound: "Ring",    title: "test", body: "test2" } 
      , { to: "ghi", sound: "vibrate", title: "test", body: "test2" }
      ]

Please Let me know how this can be done in JavaScript ?请让我知道如何在 JavaScript 中完成此操作?

You could take a non mutate approach and get a new array with new objects without altering the given data.您可以采用非变异方法并在不更改给定数据的情况下获取包含新对象的新数组。

 var tokens = [{ to: "abc", sound: "default" }, { to: "def", sound: "Ring" }, { to: "ghi", sound: "vibrate" }], result = tokens.map(o => ({ ...o, title: "test", body: "test2" })); console.log(result);

you can also use this way你也可以用这种方式

 const tokens = [ { to: "abc", sound: "default" } , { to: "def", sound: "ring" } , { to: "ghi", sound: "vibrate" } ] tokens.forEach(e=>{ e.title= "test"; e.body= "test2" }) console.log( tokens )
 .as-console-wrapper { max-height: 100% !important; top: 0; }

an other way:其它的办法:

 const tokens = [ { to: "abc", sound: "default" } , { to: "def", sound: "ring" } , { to: "ghi", sound: "vibrate" } ] newElements = {title:'test', body:'test2'} ; tokens.forEach(e=>Object.assign(e, newElements)) console.log( tokens )
 .as-console-wrapper { max-height: 100% !important; top: 0; }

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

相关问题 如何使用 JavaScript 或 jQuery 更改数组内的对象的值? - How to change value of object which is inside an array using JavaScript or jQuery? setstate object:{array:[]} in reactjs 如何在 state 对象**中的数组中添加**键和值? - setstate object:{array:[]} in reactjs how could i add the **key and value inside the array which is in the state object**? 如何向文件数组中的对象添加值? - How to add a value to an object inside a array of files? 如何在 JavaScript 中访问数组中对象的值? - How to access the value of an object inside an array in JavaScript? 如何获取JavaScript数组对象内部的对象? - How to get object which is inside array object in javascript? 如何在对象数组中存在的数组中添加值? - How to add value in array which is present inside objects array? Javascript ES6,如何向新复制的对象内的数组添加值? - Javascript ES6, how to add value to array inside a newly copied object? 如何遍历 javascript 中数组内的嵌套 object - how to iterate through nested object which is inside an array in javascript 如何在地图功能内部的对象中添加值? - How to add a value into an object which is inside map function? 如何访问在 javascript 的 object 的键内定义的 function 中的值 - how to access the value in a function which is defined inside a key of an object in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM