javascript - How to substitute all values of a certain key in an array of JSON objects -
i have array of json object, example, people
data, example:
[ { name : 'john', age : '7' }, { name : 'mary', age : '70' }, { name : 'joe', age : '40' }, { name : 'jenny', age : '4' } ]
i want substitute string values in age
corresponding integer in order sort age
. or add key, example agei
integer value.
i loop through array, but, there better way that, example 1 command in jquery?
you can use foreach
modify array
in place:
var array = [ { name : 'john', age : '7' }, { name : 'mary', age : '70' }, { name : 'joe', age : '40' }, { name : 'jenny', age : '4' } ] array.foreach(obj => { obj.age = number(obj.age) }); console.log(array);
or use map
make new array:
var array = [ { name : 'john', age : '7' }, { name : 'mary', age : '70' }, { name : 'joe', age : '40' }, { name : 'jenny', age : '4' } ] console.log( array.map(obj => ({ name: obj.name, age: number(obj.age) })) );
Comments
Post a Comment