How to iterate json array – JavaScript?

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

To iterate JSON array, use the JSON.parse().

Example

Following is the code −

var apiValues =
   [
      '{"name": "John", "scores": [78, 89]}',
      '{"name": "David", "scores": [58, 98]}',
      '{"name": "Bob", "scores": [56, 79]}',
      '{"name": "Mike", "scores": [94, 91]}'
   ];
var parseJSONObject = apiValues.map(obj => JSON.parse(obj));
console.log("The original String : ", apiValues);
console.log("The JSON Objects : ", parseJSONObject);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo252.js.

Output

This will produce the following output on console −

PS C:\Users\Amit\javascript-code> node demo252.js
The original String :  [
   '{"name": "John", "scores": [78, 89]}',
   '{"name": "David", "scores": [58, 98]}',
   '{"name": "Bob", "scores": [56, 79]}',
   '{"name": "Mike", "scores": [94, 91]}'
]
The JSON Objects :  [
   { name: 'John', scores: [ 78, 89 ] },
   { name: 'David', scores: [ 58, 98 ] },
   { name: 'Bob', scores: [ 56, 79 ] },
   { name: 'Mike', scores: [ 94, 91 ] }
]
raja
Published on 09-Nov-2020 11:33:40
Advertisements