Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
123
rated 0 times [  128] [ 5]  / answers: 1 / hits: 62583  / 8 Years ago, sun, april 17, 2016, 12:00:00

I have the following array of objects:



var array = [ {'a': '12', 'b':'10'}, {'a': '20', 'b':'22'} ];


How can I add a new property c = b - a to all objects of the array?


More From » javascript

 Answers
3

Use forEach function:



var array = [{ 'a': '12', 'b': '10' }, { 'a': '20', 'b': '22' }];

array.forEach(e => e.c = +e.b - +e.a);

console.log(JSON.stringify(array));




[#62517] Friday, April 15, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brodyfrancisi

Total Points: 1
Total Questions: 102
Total Answers: 89

Location: Marshall Islands
Member since Mon, May 31, 2021
3 Years ago
brodyfrancisi questions
;