0
[{
    "_id": "5fd6288a155cda5a10d067fa",
    "contacts": [
        {
            "_id": "5fd6288a155cda5a10d067fb",
            "id": "5fd35931ec23f76d387d8464",
            "name": "dean"
        },
        {
            "_id": "5fd6288a155cda5a10d067fc",
            "id": "12",
            "name": "john"
        },
        {
            "_id": "5fd6288a155cda5a10d067fd",
            "id": "1",
            "name": "brad"
        }
    ],
    "messages": [],
    "__v": 0
},
{
    "_id": "5fd63ab97aac3826f8e64558",
    "contacts": [
        {
            "_id": "5fd63ab97aac3826f8e64559",
            "id": "5fd35931ec23f76d387d8464",
            "name": "dean"
        },
        {
            "_id": "5fd63ab97aac3826f8e6455a",
            "id": "12",
            "name": "brad"
        }
    ],
    "messages": [],
    "__v": 0
}]

Quick overview: each object contains a different conversation data. I want to extract the names out of each conversation and store them in a separate array. for instance: [[dean, john, brad], [dean, brad]]

2
  • show what you have tried please so we can help you fix it Commented Dec 13, 2020 at 16:23
  • You have names with different ID's are they different names/people? Can a person appear more than once in a list/conversation? Commented Dec 13, 2020 at 16:26

2 Answers 2

2

const arr = [{
    "_id": "5fd6288a155cda5a10d067fa",
    "contacts": [{
        "_id": "5fd6288a155cda5a10d067fb",
        "id": "5fd35931ec23f76d387d8464",
        "name": "dean"
      },
      {
        "_id": "5fd6288a155cda5a10d067fc",
        "id": "12",
        "name": "john"
      },
      {
        "_id": "5fd6288a155cda5a10d067fd",
        "id": "1",
        "name": "brad"
      }
    ],
    "messages": [],
    "__v": 0
  },
  {
    "_id": "5fd63ab97aac3826f8e64558",
    "contacts": [{
        "_id": "5fd63ab97aac3826f8e64559",
        "id": "5fd35931ec23f76d387d8464",
        "name": "dean"
      },
      {
        "_id": "5fd63ab97aac3826f8e6455a",
        "id": "12",
        "name": "brad"
      }
    ],
    "messages": [],
    "__v": 0
  }
];

const people = arr.map(ele => ele.contacts.map(contact => contact.name));
console.log(people);

Map the array:

const people = arr.map(ele => ele.contacts.map(contact => contact.name));
Sign up to request clarification or add additional context in comments.

Comments

-1

You can map over the elements and extract the data out.

var your_elements_array = [{
    "_id": "5fd6288a155cda5a10d067fa",
    "contacts": [
        {
            "_id": "5fd6288a155cda5a10d067fb",
            "id": "5fd35931ec23f76d387d8464",
            "name": "dean"
        },
        {
            "_id": "5fd6288a155cda5a10d067fc",
            "id": "12",
            "name": "john"
        },
        {
            "_id": "5fd6288a155cda5a10d067fd",
            "id": "1",
            "name": "brad"
        }
    ],
    "messages": [],
    "__v": 0
},
{
    "_id": "5fd63ab97aac3826f8e64558",
    "contacts": [
        {
            "_id": "5fd63ab97aac3826f8e64559",
            "id": "5fd35931ec23f76d387d8464",
            "name": "dean"
        },
        {
            "_id": "5fd63ab97aac3826f8e6455a",
            "id": "12",
            "name": "brad"
        }
    ],
    "messages": [],
    "__v": 0
}];
let x = your_elements_array.map(element => {
  return element["contacts"].map(element => {
    return element["name"];
  });
});
console.log(x);

your_elements_array.map(element => {
  return element["contacts"].map(element => {
    return element["name"];
  });
});

2 Comments

what is /* CODE */ since this is what the question is about, please provide that
@MarkSchultheiss I left that space empty because I wanted Dean_Geva to store those names in an array and then later append that array into the array Dean_Geva wanted at the last.

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.