简体   繁体   English

使用es6获取对象中对象中的值作为数组

[英]get values in an object in object as array with es6

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 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": "lalacifay@cliptik.net",
    "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']我想要实现的是只获取areaCodes对象中的name属性,例如['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);

Is this what you are looking for?这是你想要的?

 const dataset = [{ "id": 482, "firstName": "Micheal", "lastName": "Bamford", "email": "lalacifay@cliptik.net", "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); } }

You can use array#flatMap and array#map to extract name from areaCodes .您可以使用array#flatMaparray#mapareaCodes提取name

 const data = [{ "id": 482, "firstName": "Micheal", "lastName": "Bamford", "email": "lalacifay@cliptik.net", "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);

And even shorter:甚至更短:

 const obj = [{ "id": 482, "firstName": "Micheal", "lastName": "Bamford", "email": "lalacifay@cliptik.net", "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)

Try with filter , reduce and map ,尝试使用filterreducemap

 const listFromAPI = [ { id: 482, firstName: "Micheal", lastName: "Bamford", email: "lalacifay@cliptik.net", 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);

试试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM