简体   繁体   English

从数组打字稿中的对象过滤数据

[英]Filter data from object inside array typescript

I have an array events as follows, 我有如下的数组事件,

[{
    "_id": "5890b4796166c457ffdee243",
    "description": "Adele",
    "name": "Adele",
    "place": {
        "name": "Houston Toyota Center",
        "location": {
            "city": "Houston",
            "country": "United States",
            "latitude": 29.751054939716,
            "longitude": -95.362142762854,
            "state": "TX",
            "street": "1510 Polk St",
            "zip": "77002",
            "_id": "58992aebf2dbf4369c0a0325"
        },
        "id": "200866860532",
        "_id": "5890b47c6166c457ffdee394"
    },
    "start_time": "2016-11-09T20:00:00-0600",
    "id": "1644669702488073"
}, {
    "_id": "5890b4796166c457ffdee242",
    "description": "Please note that delivery will be delayed on all tickets until Friday January 8, 2016. Please adhere to the published ticket limits, which will be strictly enforced. If you exceed these limits, you may have any or all of your orders and tickets cancelled without notice. Please note: Every person, regardless of age, must have a ticket to be admitted to this event. RAIL RIDE EVENT: When you purchase a ticket to a Talking Stick Resort Arena event, you can ride the METRO LIGHT RAIL at no cost for four hours prior to the event through the end of the transit day.",
    "name": "Adele",
    "place": {
        "name": "Talking Stick Resort Arena",
        "location": {
            "city": "Phoenix",
            "country": "United States",
            "latitude": 33.445995372225,
            "longitude": -112.07135782626,
            "state": "AZ",
            "street": "201 E Jefferson St",
            "zip": "85004",
            "_id": "58992aebf2dbf4369c0a0327"
        },
        "id": "53475637527",
        "_id": "5890b4856166c457ffdee427"
    },
    "start_time": "2016-11-21T19:30:00-0700",
    "id": "905384112862937"
}, {
    "_id": "5890b4796166c457ffdee24a",
    "description": "Delivery of tickets will be delayed until 12/31/15",
    "name": "Adele",
    "place": {
        "name": "AmericanAirlines Arena",
        "location": {
            "city": "Miami",
            "country": "United States",
            "latitude": 25.781236943411,
            "longitude": -80.188316709574,
            "state": "FL",
            "street": "601 Biscayne Blvd",
            "zip": "33132",
            "_id": "58992aebf2dbf4369c0a0329"
        },
        "id": "120400119061",
        "_id": "5890b4946166c457ffdee464"
    },
    "start_time": "2016-10-25T19:30:00-0400",
    "id": "445046279020601"
}, {
    "_id": "5890b4796166c457ffdee244",
    "description": "Adele",
    "name": "Adele",
    "place": {
        "name": "Houston Toyota Center",
        "location": {
            "city": "Houston",
            "country": "United States",
            "latitude": 29.751054939716,
            "longitude": -95.362142762854,
            "state": "TX",
            "street": "1510 Polk St",
            "zip": "77002",
            "_id": "58992aebf2dbf4369c0a032b"
        },
        "id": "200866860532",
        "_id": "5890b47c6166c457ffdee354"
    },
    "start_time": "2016-11-08T20:00:00-0600",
    "id": "1662607760654203"
}, {
    "_id": "5890b4796166c457ffdee245",
    "description": "Delivery will be delayed until Oct 2, 2016.",
    "name": "Adele",
    "place": {
        "name": "American Airlines Center",
        "location": {
            "city": "Dallas",
            "country": "United States",
            "latitude": 32.790485550848,
            "longitude": -96.810278349053,
            "state": "TX",
            "street": "2500 Victory Ave",
            "zip": "75219",
            "_id": "58992aebf2dbf4369c0a032d"
        },
        "id": "26606856232",
        "_id": "5890b47b6166c457ffdee2e4"
    },
    "start_time": "2016-11-02T20:00:00-0500",
    "id": "649884741817020"
}]

How to get all the city from the above json using typescript? 如何使用打字稿从上述json获取所有城市?

i have tried this, 我已经试过了

this.eventsFiltered = this.events.filter(
          book => book.place.location.city);

try this: 尝试这个:

this.eventsFiltered = this.events.map(
              book => book.place.location.city);

EDIT 编辑

i want to filter the events which has speficic places? 我想过滤具有特殊位置的事件?

 let events = [{ "_id": "5890b4796166c457ffdee243", "description": "Adele", "name": "Adele", "place": { "name": "Houston Toyota Center", "location": { "city": "Houston", "country": "United States", "latitude": 29.751054939716, "longitude": -95.362142762854, "state": "TX", "street": "1510 Polk St", "zip": "77002", "_id": "58992aebf2dbf4369c0a0325" }, "id": "200866860532", "_id": "5890b47c6166c457ffdee394" }, "start_time": "2016-11-09T20:00:00-0600", "id": "1644669702488073" }, { "_id": "5890b4796166c457ffdee242", "description": "Please note that delivery will be delayed on all tickets until Friday January 8, 2016. Please adhere to the published ticket limits, which will be strictly enforced. If you exceed these limits, you may have any or all of your orders and tickets cancelled without notice. Please note: Every person, regardless of age, must have a ticket to be admitted to this event. RAIL RIDE EVENT: When you purchase a ticket to a Talking Stick Resort Arena event, you can ride the METRO LIGHT RAIL at no cost for four hours prior to the event through the end of the transit day.", "name": "Adele", "place": { "name": "Talking Stick Resort Arena", "location": { "city": "Phoenix", "country": "United States", "latitude": 33.445995372225, "longitude": -112.07135782626, "state": "AZ", "street": "201 E Jefferson St", "zip": "85004", "_id": "58992aebf2dbf4369c0a0327" }, "id": "53475637527", "_id": "5890b4856166c457ffdee427" }, "start_time": "2016-11-21T19:30:00-0700", "id": "905384112862937" }, { "_id": "5890b4796166c457ffdee24a", "description": "Delivery of tickets will be delayed until 12/31/15", "name": "Adele", "place": { "name": "AmericanAirlines Arena", "location": { "city": "Miami", "country": "United States", "latitude": 25.781236943411, "longitude": -80.188316709574, "state": "FL", "street": "601 Biscayne Blvd", "zip": "33132", "_id": "58992aebf2dbf4369c0a0329" }, "id": "120400119061", "_id": "5890b4946166c457ffdee464" }, "start_time": "2016-10-25T19:30:00-0400", "id": "445046279020601" }, { "_id": "5890b4796166c457ffdee244", "description": "Adele", "name": "Adele", "place": { "name": "Houston Toyota Center", "location": { "city": "Houston", "country": "United States", "latitude": 29.751054939716, "longitude": -95.362142762854, "state": "TX", "street": "1510 Polk St", "zip": "77002", "_id": "58992aebf2dbf4369c0a032b" }, "id": "200866860532", "_id": "5890b47c6166c457ffdee354" }, "start_time": "2016-11-08T20:00:00-0600", "id": "1662607760654203" }, { "_id": "5890b4796166c457ffdee245", "description": "Delivery will be delayed until Oct 2, 2016.", "name": "Adele", "place": { "name": "American Airlines Center", "location": { "city": "Dallas", "country": "United States", "latitude": 32.790485550848, "longitude": -96.810278349053, "state": "TX", "street": "2500 Victory Ave", "zip": "75219", "_id": "58992aebf2dbf4369c0a032d" }, "id": "26606856232", "_id": "5890b47b6166c457ffdee2e4" }, "start_time": "2016-11-02T20:00:00-0500", "id": "649884741817020" }]; let eventsFiltered = events.map( book => book.place.location.city); console.log(eventsFiltered); let city = 'Houston'; let eventsCt = events.filter(book => book.place.location.city == city); console.log('2nd question'); console.log(eventsCt); 

EDIT 2 编辑2

i have a field named "parent" which is an array of string. 我有一个名为“父”的字段,它是一个字符串数组。 i already have a string named "12344" which is a part of array parent. 我已经有一个名为“ 12344”的字符串,它是数组父级的一部分。 how do i check if the events.parent cotains this string and filter those objects 我如何检查events.parent是否包含此字符串并过滤这些对象

let p = '1234';
let eventsF = events.filter(book => book.parent && Array.isArray(book.parent)&& book.parent.indexOf(p) !== -1);

Filter used for filter records with where conditions . 筛选器用于筛选where conditionswhere conditions记录。 In your case you want transform data and you just want a array of city from an array of object. 在您的情况下,您需要转换数据,而您只需要一个对象数组中的一个城市数组。 So in this case use map instance of filter 因此,在这种情况下,请使用filter map实例

Example

this.cityList = this.events.map(book => book.place.location.city);

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

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