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

135
Views
JS: Extracting data from an array of objects

I have a complex query with 100s of fields and nested fields. What I want to do is, for each Index, extract the English and French text. As you can see in the array, there is no French text for some indexes. In that case I want to get the English text. For me extracting the English text works fine because the text is already there, but incase of French, I get undefined errors. What would be the best way to implement this. Is Loadash needed for this or just pure JS methods?

Just to be clear, I have erros with extracting french because in some fields, french text is not available, I want to use the english value in that case.

Also It is recommend if I am able to get the English and French values by it's language field rather than the index. I have no idea how to do that.

Any suggestion, documentation is appreciated. Thank you!

example array:

[
 {
    id: "1",
    name: [
      {
        language: "en-US",
        text: "HOLIDAY"
      }
    ],
    order: 6,
    Groups: [
      {
        name: [
          {
            language: "en-US",
            text: "REGULAR"
          }
        ],
        code: "REGEARN"
      },
      {
        name: [
          {
            language: "en-US",
            text: "CHARGE"
          }
        ],
        code: "CHARGE"
      }
    ]
  }
]

and here is the code sandbox that reproduces my error: CODE SAND BOX https://codesandbox.io/s/javascript-forked-5073j

EDIT:

EXPECTED OUTPUT:

{
  key: key,
  englishtext: "Value Here",
  frenchtext: "Value Here"
}

below is a working code, but issue is it does not work when there is no french language or that field. I get undefined errors. So is it possible I can get the needed data from the language field?

x.map((y) => ({
    key: y.id,
    name: y.name[0].text,
    groupname: y.Groups ? x.Groups[0].name?.[0].text : 'N/A',
  }))
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Do you expect result like this? If you don't mind lodash.

const _ = require('lodash');

const getNames = (arr) => {
  return arr.map((obj) => {
    const id = obj.id;
    const englishtext = _.get(obj, 'name[0].text', 'N/A');
    const frenchtext = _.get(obj, 'name[1].text', englishtext);

    return { id, englishtext, frenchtext };
  });
};
        
console.log(getNames(x));
// [
//   { id: '1', englishtext: 'HOLIDAY', frenchtext: 'HOLIDAY' },
//   { id: '2', englishtext: 'Stat Holiday', frenchtext: 'Congé Férié' },
//   { id: '3', englishtext: 'Over', frenchtext: 'Over' }
// ] 
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!