javascript-将新属性推入数组中的对象 [英] javascript - pushing new property to objects that are in an array
本文介绍了javascript-将新属性推入数组中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组人,其中包含许多看起来像这样的对象:
I got an array person containing many objects that look like this:
const person = [
{ first: 'firstName', last: 'lastName', year: 1439, passed: 1495 },
...
]
我已经计算出该人的寿命:
I have counted how many years the person lived:
const oldest = person.map(x => x.passed - x.year);
每个人的岁月都有新的数组.
Got new array with the years for every person.
现在,我想将此计算的年份作为新的属性年龄推入此数组中的每个人对象.
Now I would like to push this calculated year as a new property age to each person object in this array.
你能帮我吗?
推荐答案
您可以添加新属性
person.forEach(p => p.lifetime = p.passed - p.year);
或映射新的对象数组
persons = person.map(p => ({ ...p, lifetime: p.passed - p.year });
这篇关于javascript-将新属性推入数组中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文