JavaScript creating an array from JSON data?

JavascriptWeb DevelopmentObject Oriented Programming

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']
raja
Published on 07-Sep-2020 11:39:15
Advertisements