1

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?

6
  • 1
    originalArray is an Array of Arrays. Arrays do not have a phoneNumber property. I don’t know why you expected this filter to work. Arrays don’t have a value property either; none of your objects do. Have you considered flattening the array first? Commented Feb 5, 2022 at 20:14
  • So why if I do typeof(originalArray) it returns 'object'? Commented Feb 5, 2022 at 20:19
  • 1
    Arrays are a special type of object and `typeof [] == 'object'`` Commented Feb 5, 2022 at 20:21
  • @Danny Have you read the documentation? See Why does typeof array with objects return "object" and not "array"?. Commented Feb 5, 2022 at 20:21
  • I will, thank you. Commented Feb 5, 2022 at 20:25

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

1

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

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

Comments

1

We can achieve this by :

  • Flattening the multidimensional array into single array.
  • looping through each object using Array.map() to fetch the phone number.
  • This step is for safe side to eliminate the empty or null phone numbers using Array.filter()

Working Demo :

let 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 flatArray = originalArray.flat().map((obj) => obj.phoneNumber).filter((item) => item);

console.log(flatArray);

Comments

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.