Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

JavaScript creating an array from JSON data?



To create an array from JSON data, use the concept of map() from JavaScript. Let’s say the following is our data −

const studentDetails =[
   {
      name : "John"
   },
   {
      name : "David"
   },
   {
      name : "Bob"
   }
];

Following is the code to create an array from the above data −

Example

const studentDetails =[
   {
      name : "John"
   },
   {
      name : "David"
   },
   {
      name : "Bob"
   }
];
const ListOfStudentName =
studentDetails.map(({name:actualValue})=>actualValue);
console.log(ListOfStudentName);

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

node fileName.js.

Here, my file name is demo82.js.

Output

This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo82.js
[ 'John', 'David', 'Bob']
Updated on: 2020-09-07T08:09:27+05:30

897 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements