0

I'm pulling data from a service that loads into a dropdown and when a value is selected it's being assigned to a variable and an example is below

[{
    "id": 482,
    "firstName": "Micheal",
    "lastName": "Bamford",
    "email": "[email protected]",
    "areaCodes": [
        {
            "id": 60,
            "name": "New York",
            "status": "ACTIVE",
            "createdDate": "2018-10-30 14:09:28"
        },
        {
            "id": 12,
            "name": "Florida",
            "status": "ACTIVE",
            "createdDate": "2018-10-30 14:09:28"
        }
    ],

    "createdDate": "2019-01-03 12:29:33"
}]

What i'm trying to achieve is to only get the name property in the areaCodes objects like this ['New York', 'Florida']

Below is the code i'm using so far which only gets me only the first in the array

const selected = this.listFromAPI.filter(t => t.id === ev.id).map(r=> r.areaCodes[0].name);
3
  • 1
    What is ev.id?.. Commented Jul 1, 2021 at 16:38
  • ev.id is the value grab from the dropdown after selection Commented Jul 1, 2021 at 16:43
  • listFromAPI is an array. Does it have other elements? Do you want names from all areaCodes from all elements, because you have filter with id in your code. Commented Jul 1, 2021 at 16:44

5 Answers 5

1

Is this what you are looking for?

const dataset = [{
  "id": 482,
  "firstName": "Micheal",
  "lastName": "Bamford",
  "email": "[email protected]",
  "areaCodes": [{
      "id": 60,
      "name": "New York",
      "status": "ACTIVE",
      "createdDate": "2018-10-30 14:09:28"
    },
    {
      "id": 12,
      "name": "Florida",
      "status": "ACTIVE",
      "createdDate": "2018-10-30 14:09:28"
    }
  ],

  "createdDate": "2019-01-03 12:29:33"
}];

for (const item of dataset) {
  const {
    areaCodes
  } = item;
  if (Array.isArray(areaCodes)) {
    const names = areaCodes.map(c => c.name);
    console.log(names);
  }
}

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

Comments

1

You can use array#flatMap and array#map to extract name from areaCodes.

const data = [{ "id": 482, "firstName": "Micheal", "lastName": "Bamford", "email": "[email protected]", "areaCodes": [{ "id": 60, "name": "New York", "status": "ACTIVE", "createdDate": "2018-10-30 14:09:28" }, { "id": 12, "name": "Florida", "status": "ACTIVE", "createdDate": "2018-10-30 14:09:28" } ], "createdDate": "2019-01-03 12:29:33" }],
    result = data.flatMap(o => o.areaCodes.map(({name}) => name));
console.log(result);

2 Comments

I think it also makes sense to use Set in order to get unique values
Yeah, it would be, but unfortunately, OP hasn't asked of unique value.
0

And even shorter:

const obj = [{
    "id": 482,
    "firstName": "Micheal",
    "lastName": "Bamford",
    "email": "[email protected]",
    "areaCodes": [
        {
            "id": 60,
            "name": "New York",
            "status": "ACTIVE",
            "createdDate": "2018-10-30 14:09:28"
        },
        {
            "id": 12,
            "name": "Florida",
            "status": "ACTIVE",
            "createdDate": "2018-10-30 14:09:28"
        }
    ],

    "createdDate": "2019-01-03 12:29:33"
}]


const res = obj[0].areaCodes.map(i=>i.name);

console.log(res)

Comments

0

Try with filter, reduce and map,

const listFromAPI = [
  {
    id: 482,
    firstName: "Micheal",
    lastName: "Bamford",
    email: "[email protected]",
    areaCodes: [
      {
        id: 60,
        name: "New York",
        status: "ACTIVE",
        createdDate: "2018-10-30 14:09:28",
      },
      {
        id: 12,
        name: "Florida",
        status: "ACTIVE",
        createdDate: "2018-10-30 14:09:28",
      },
    ],

    createdDate: "2019-01-03 12:29:33",
  },
];

// Value grabbed from drop down selector
const ev = {
  id: 482,
};

const result = listFromAPI
  .filter((t) => t.id === ev.id)
  .reduce((temp, currentVal) => {
    if (currentVal.areaCodes && Array.isArray(currentVal.areaCodes)) {
      currentVal.areaCodes.map((el) => {
        temp.push(el.name);
      });
    }

    return temp;
  }, []);

console.log(result);

Comments

-1

Try this one

const selected = this.listFromAPI.filter(t => t.id === ev.id).map(r => r.areaCodes.map(areaCode => areaCode.name));

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.