0

So imagine having a JSON file like:

[
  { "name": "David", "code": "DA" },
  { "name": "Solomon", "code": "SO" },
  { "name": "Mirage", "code": "MI" }
]

How can I convert it to something like this: ['David', 'Solomon', 'Mirage']and ['DA', 'SO', 'MI]

3

2 Answers 2

3
let data = [
 { "name": "David", "code": "DA" },
 { "name": "Solomon", "code": "SO" },
 { "name": "Mirage", "code": "MI" }
];

 const result = data.map(item => item.name);
 const result2 = data.map(item => item.code);

 console.log(result, result2);
Sign up to request clarification or add additional context in comments.

Comments

0
let data = [
    { "name": "David", "code": "DA" },
    { "name": "Solomon", "code": "SO" },
    { "name": "Mirage", "code": "MI" }
 ];
  let a1= [];
  let a2= [];
  data.forEach(ei =>{
    a1.push(ei.name);
    a2.push(ei.code);
  })
  console.log(a1);
  console.log(a2);

1 Comment

If you do not wish to return value, use .forEach instead. .map returns n length array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.