I have the following array of objects:
var array = [ {'a': '12', 'b':'10'}, {'a': '20', 'b':'22'} ];
How to add a new property c = b - a to all objects of the array?
c = b - a
Use the forEach :
forEach
var array = [{ 'a': '12', 'b': '10' }, { 'a': '20', 'b': '22' }]; array.forEach(function(e) { ec = +eb - +ea }); document.write(JSON.stringify(array));
you can use array.map,
and you should use Number () to convert props to numbers to add:
var array = [ {'a': '12', 'b':'10'}, {'a': '20', 'b':'22'} ]; var r = array.map( x => { xc = Number(xb) - Number(xa); return x }) console.log(r)
Source: https://habr.com/ru/post/1247311/More articles:iOS XMPP never receives any friend / registry requests - iosCounter with lambda over java8 card - javahttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1247308/map-a-numpy-array-of-strings-to-integers&usg=ALkJrhj8s93gQdNvrtB7ZvTVrG9PHcuOXQC: Pass two values ββseparated by commas using one #define - cHow can I change a project in BigQuery - google-bigqueryAutomatically add docker container to top nginx load balancing configuration - dockerHow to add spacing between UITableViewCells - Swift - iosFixed work in Java: how to access files from "resources" when running in a cluster - javaMeasuring the size of a function created using Clang / LLVM? - cHow to open 2 instances of Visual Studio with the same Git projects and different branches - gitAll Articles