Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

89
Views
I had some issue in JSON data and i can get only name in Andrew and Karthick
const data=[{
    "name": "Andrew",
    "age": "23",
    "subject": [{
        "name": "english",
        "period": "1st"
    }]
},
{
    "name": "karthick",
    "age": "29",
    "subject": [{
        "name": "French",
        "period": "3st"
    }]
}]

I need to console only the name value in an array and print in console Example:["Andrew","english","karthick","French"]

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Using Array#flatMap and Array#map:

const data = [ { "name": "Andrew", "age": "23", "subject": [ { "name": "english", "period": "1st" } ] }, { "name": "karthick", "age": "29", "subject": [ { "name": "French", "period": "3st" } ] } ];

const res = data.flatMap(({ name, subject = [] }) => ([
  name,
  ...subject.map(({ name }) => name)
]));

console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

Based on your example dataset you don't have to do anything complicated here. Loop over the data and then push the name and subject name into a new array.

const data=[{name:"Andrew",age:"23",subject:[{name:"english",period:"1st"}]},{name:"karthick",age:"29",subject:[{name:"French",period:"3st"}]}];

const arr = [];

for (const obj of data) {
  arr.push(obj.name);
  arr.push(obj.subject[0].name);
}

console.log(arr);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!