简体   繁体   中英

get ids from json array

I'm having a hard time trying to figure out how to iterate through json data to gather all the duplicate Id's to display the "dateTimes" that match their Id.

在此处输入图像描述

for example, I need it to display similar to this: Regal Broward Stadium 12 & RPX 2017-09-20 13:30 2017-09-20 16:00 2017-09-20 18:35

Flipper's Hollywood Cinema 10 2017-09-20 12:40 2017-09-20 14:40 2017-09-20 16:35

I wrote a function that I think that would work for just one Id but I don't know how I would find all the matching Id's to display the dateTimes.

getShowtimes(data){

    var json = JSON.parse(data);

    for(let i = 0; json.theater.id; i++){

        if (json[i].theatre.id == 10863){

            json[i].theatre.dateTime;

        }

    }

}

right now i'm not using the function to display the results(cause it doesn't work ), I'm just using the code below.

<div class="showtime" *ngFor="let shows of show">
    <div *ngFor="let detail of shows.showtimes>
        <div *ngIf="detail.theatre.id == 10863">
            {{detail.theatre.name}}{{detail.dateTime}}
        </div>
    </div>
</div>

This could be a good solution:

let employees = [
    { "id": 1, "firstName":"John", "lastName":"Doe" },
    { "id": 2, "firstName":"Anna", "lastName":"Smith" },
    { "id": 3, "firstName":"Anna", "lastName":"Smith" },
    { "id": 4, "firstName":"Peter", "lastName":"Jones" }
];
    
let employeeIds = [];
employeeIds.push(...employees.map(emp => emp.id));
console.log(employeeIds); 

output of employeeIds is [ 1, 2, 3, 4 ]

let duplicateNames = employees
     .map(emp => emp['firstName'])
     .map((emp, i, final) => final.indexOf(emp) !== i && i)
     .filter(obj=> employees[obj])
     .map(emp => employees[emp]["firstName"])
console.log(duplicateNames); 

output of duplicateNames is [ 'Anna' ]

Maybe this can help:

 const data = [ { theatre: { id: "10863", name: "Regal Broward Stadium 12 & RPX", dateTime: "2017-09-20T13:30" }}, { theatre: { id: "10863", name: "Regal Broward Stadium 12 & RPX", dateTime: "2017-09-20T13:30" }}, { theatre: { id: "10863", name: "Regal Broward Stadium 12 & RPX", dateTime: "2017-09-20T16:00" }}, { theatre: { id: "10863", name: "Regal Broward Stadium 12 & RPX", dateTime: "2017-09-20T18:35" }}, { theatre: { id: "10863", name: "Regal Broward Stadium 12 & RPX", dateTime: "2017-09-20T21:00" }}, { theatre: { id: "4089", name: "Flipper's Hollywood Cinema 10", dateTime: "2017-09-20T12:40" }}, { theatre: { id: "4089", name: "Flipper's Hollywood Cinema 10", dateTime: "2017-09-20T14:40" }}, { theatre: { id: "4089", name: "Flipper's Hollywood Cinema 10", dateTime: "2017-09-20T16:35" }} ]; let result = {}; data.forEach((item) => { if (!result.hasOwnProperty(item.theatre.id)) { result[item.theatre.id] = { name: item.theatre.name, dates: item.theatre.dateTime }; } else { result[item.theatre.id].dates = result[item.theatre.id].dates + ' ' + item.theatre.dateTime; } }); Object.keys(result).forEach((key) => { console.log(`${result[key].name} ${result[key].dates}`) }); 

Use like this detail.theatre.id === '10863' because its a string,

<div *ngIf="detail.theatre.id === '10863'">
    {{detail.theatre.name}}{{detail.dateTime}}
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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