How do I map over a contacts array to produce a new array with objects and properties?

LoF10

I am looking to map over an array to create a new array with relevant properties pulled out. I have successfully accessed my contacts on my phone and want to push the data into a more organized structure. How do I map over Array In to get to Array Out using the first values for email and phone numbers if they are present? Below is an example of a contacts array:

Array In

let arrayIn=  [
    {phoneNumbers:[
        { label: 'work',  number: '+3476859087'},
        { label: 'mobile', number: '+4567893214'}
        ],
        lookupKey:"12345",
        company:"PHONE",firstName:"John",contactType:"person",name:"John Smith",id:"879",
        emails:[
            {email:'[email protected]'}
        ],
        lastName:"Smith",
    },
    {phoneNumbers:[
        { label: 'mobile', number: '+3476859087'},
        { label: 'work', number: '+4567773214'}
        ],
        lookupKey:"890744",
        company:"PHONE",firstName:"Carl",name:"Carl Johnson",id:"879",
        emails:[
            {email:'[email protected]'}
        ],
        lastName:"johnson",
    }
    ]

I would like to end up with the following: Array out

[
      {name: 'John Smith', phone: 3476859087, email: '[email protected]'},
      {name: 'Carl Johnson', phone: 3476859087, email: '[email protected]'}
    ]

I've mapped over the array to create a contact list and as I select each one I want to push it into the state. My snack is here for reference: https://snack.expo.io/@fauslyfox110/testingreferrals

Not looking for anyone to do my snack for me, just to help me with the array portion to go from array In to array out.

Answer that worked

let arrayOut = arrayIn.reduce((acc, {name, phoneNumbers, emails}) => {
  return [...acc, {
    'name': name,
    'phone': phoneNumbers[0]['number'].replace('+', ''),
    'email': emails[0].email
  }];
}, []);

console.log(arrayOut);
Shiny

You had some Sytax errors in your data, mainly that you were using [] and then assigning key:value pairs, which would belong in an object - I've swapped the [] for {} in this case

I've used a .reduce() to iterate through your data and return the desired Array. I wasn't sure how you wanted the phone number handled, so I'm returning just the first one

const arrayIn = [{
    phoneNumbers: [{
      label: 'work',
      number: '+3476859087'
    }, {
      label: 'mobile',
      number: '+4567893214'
    }],
    lookupKey: "12345",
    company: "PHONE",
    firstName: "John",
    contactType: "person",
    name: "John Smith",
    id: "879",
    emails: [{
      email: '[email protected]'
    }],
    lastName: "Smith",
  },
  {
    phoneNumbers: [{
      label: 'mobile',
      number: '+3476859087'
    }, {
      label: 'work',
      number: '+4567773214'
    }],
    lookupKey: "890744",
    company: "PHONE",
    firstName: "Carl",
    name: "Carl Johnson",
    id: "879",
    emails: [{
      email: '[email protected]'
    }],
    lastName: "johnson",
  }
]

let arrayOut = arrayIn.reduce((acc, {
  name,
  phoneNumbers,
  emails
}) => {
  return [...acc, {
    'name': name,
    'phone': phoneNumbers[0]['number'].replace('+', ''),
    'email': emails[0].email
  }];
}, []);

console.log(arrayOut);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How do I loop over an array of objects to return a property dependent on a corresponding property within that object?

分類Dev

How do i push objects into the same array?

分類Dev

How do i filter an array inside of a array of objects?

分類Dev

How do I use array-contains for array containing map

分類Dev

How do you map an array of objects within an object?

分類Dev

How do I optimally distribute values over an array of percentages?

分類Dev

How do I create an boolean equals method compares objects in an array

分類Dev

How do I convert array of objects to object in javascript?

分類Dev

How do I create a react list component from an array of objects?

分類Dev

How do I display array objects using a simple loop and 'if' statements?

分類Dev

How do I format an array of objects by iteration in Model in Ruby on Rails

分類Dev

how do i sort an array of objects via the comparable interface?

分類Dev

How can I get a new array filtered by value in another array of objects? javascript

分類Dev

How can I slice out all of the array objects containing a value and storing them in a new array

分類Dev

How to merge some properties in array of objects into another array?

分類Dev

JAXB - how to map xml array to list of Objects

分類Dev

Ruby on Rails: How can I use JSONPath to access (and save to database) nested objects/properties within a JSON array?

分類Dev

Mapping array of objects into new array of array by value

分類Dev

Is array preferred over set or map?

分類Dev

How do I save objects that I push into an array, then display them using local storage?

分類Dev

How can I display an array of objects?

分類Dev

how to iterate over a dictionary from value in an array and store in a new dictionary

分類Dev

lodash map returning array of objects

分類Dev

Compare an Array to an Array of Objects and Return a new Array of Objects with Grouped Dates

分類Dev

How do I sync contacts on Ubuntu Touch?

分類Dev

How do I sync Google contacts?

分類Dev

How do I map functions over a RoseTree in Applicative (Haskell)?

分類Dev

'push' new number into an array of objects

分類Dev

How do I deploy REST API using an imported array of JS objects?

Related 関連記事

  1. 1

    How do I loop over an array of objects to return a property dependent on a corresponding property within that object?

  2. 2

    How do i push objects into the same array?

  3. 3

    How do i filter an array inside of a array of objects?

  4. 4

    How do I use array-contains for array containing map

  5. 5

    How do you map an array of objects within an object?

  6. 6

    How do I optimally distribute values over an array of percentages?

  7. 7

    How do I create an boolean equals method compares objects in an array

  8. 8

    How do I convert array of objects to object in javascript?

  9. 9

    How do I create a react list component from an array of objects?

  10. 10

    How do I display array objects using a simple loop and 'if' statements?

  11. 11

    How do I format an array of objects by iteration in Model in Ruby on Rails

  12. 12

    how do i sort an array of objects via the comparable interface?

  13. 13

    How can I get a new array filtered by value in another array of objects? javascript

  14. 14

    How can I slice out all of the array objects containing a value and storing them in a new array

  15. 15

    How to merge some properties in array of objects into another array?

  16. 16

    JAXB - how to map xml array to list of Objects

  17. 17

    Ruby on Rails: How can I use JSONPath to access (and save to database) nested objects/properties within a JSON array?

  18. 18

    Mapping array of objects into new array of array by value

  19. 19

    Is array preferred over set or map?

  20. 20

    How do I save objects that I push into an array, then display them using local storage?

  21. 21

    How can I display an array of objects?

  22. 22

    how to iterate over a dictionary from value in an array and store in a new dictionary

  23. 23

    lodash map returning array of objects

  24. 24

    Compare an Array to an Array of Objects and Return a new Array of Objects with Grouped Dates

  25. 25

    How do I sync contacts on Ubuntu Touch?

  26. 26

    How do I sync Google contacts?

  27. 27

    How do I map functions over a RoseTree in Applicative (Haskell)?

  28. 28

    'push' new number into an array of objects

  29. 29

    How do I deploy REST API using an imported array of JS objects?

ホットタグ

アーカイブ