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

72
Views
Building array from object of arrays returns undefined

So I have this originalArray:

[
  [
    {
       “name”: “Selena”,
       “lastName”: “Gomez”,
       “likes”: {
           “color”: “red”,
           “country”: “Argentina”,
           “state”: {
                “name”: “Buenos Aires”
           }
        },
        “phoneNumber”: “5555555555”,
        “isAvailable”: false
    }
  ],
   [
    {
       “name”: “Nick”,
       “lastName”: “Jonnas”,
       “likes”: {
          “color”: “blue”,
          “country”: “Argentina”,
          “state”: {
            “name”: “Buenos Aires”
          }
        },
        “phoneNumber”: “7777777777”,
        “isAvailable”: true
      },
       {
       “name”: “Joe”,
       “lastName”: “Jonnas”,
       “likes”: {
            “color”: “yellow”,
            “country”: “Argentina”,
            “state”: {
                 “name”: “Buenos Aires”
            }
        },
        “phoneNumber”: “9999999999”,
        “isAvailable”: false
      }
    ]
]

As you can see is an Object that has the form of (2) [Array(1), Array(2)] and I want to iterate over the object and get only the phoneNumbers items.

Expected output:

['55555555555', '7777777777', '9999999999']

I've tried:

const newArray= originalArray.map(element => element.phoneNumber);

But it returns undefined, actually I tried accessing with . like originalArray.phoneNumber and shows undefined too.

I also tried with this approach with no result:

var newArray = originalArray.filter(obj => {
  return obj.phoneNumber != null
})

Is there a way to iterate over the object, look up the phoneNumber key, and add those results to a new array?

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

0

const phoneNumbers = originalArray.flat().map(element => element.phoneNumber);

about 4 years ago · Juan Pablo Isaza Report

0

You were close, just needed to add map. Try this

var newArray = originalArray.flat()
.filter(obj => obj.phoneNumber!=null)
.map(obj => { return obj.phoneNumber;});
about 4 years ago · Juan Pablo Isaza Report

0

const originalArray = [
  [
    {
      name: "Selena",
      lastName: "Gomez",
      likes: {
        color: "red",
        country: "Argentina",
        state: {
          name: "Buenos Aires",
        },
      },
      phoneNumber: "5555555555",
      isAvailable: false,
    },
  ],
  [
    {
      name: "Nick",
      lastName: "Jonnas",
      likes: {
        color: "blue",
        country: "Argentina",
        state: {
          name: "Buenos Aires",
        },
      },
      phoneNumber: "7777777777",
      isAvailable: true,
    },
    {
      name: "Joe",
      lastName: "Jonnas",
      likes: {
        color: "yellow",
        country: "Argentina",
        state: {
          name: "Buenos Aires",
        },
      },
      phoneNumber: "9999999999",
      isAvailable: false,
    },
  ],
];

const phoneNums = originalArray.flat().map((element) => element.phoneNumber);

console.log(phoneNums);

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!