admin管理员组文章数量:1419199
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>
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>
Share
Improve this question
edited Sep 20, 2017 at 1:34
Axel
3,32311 gold badges37 silver badges60 bronze badges
asked Sep 20, 2017 at 1:27
JasonJason
671 silver badge7 bronze badges
1
- you need to take the flat array data and do a group by id. you can use library like lodash for this purpose or write a native javascript method for grouping stackoverflow./questions/14446511/… – spiritwalker Commented Sep 20, 2017 at 1:34
3 Answers
Reset to default 3This 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>
本文标签: javascriptget ids from json arrayStack Overflow
版权声明:本文标题:javascript - get ids from json array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745304703a2652575.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论