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

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -