gpt4 book ai didi

javascript - 根据提供的值过滤嵌套键值对

转载 作者:行者123 更新时间:2023-12-01 02:19:25 26 4
gpt4 key购买 nike

我是 Angular 新手,第一次做键值对。我试图根据嵌套键值映射内的特定值获取键值对。我有一个嵌套的 JSON 数据格式:

trips = {
"20180201": [{
"journeyId": 1001,
"Number": "001",
"DriverName": "Alex",
"Transporter": {
"id": "T1",
"number": "AN01001",
"Company": "Tranzient"
},
"place": [{
"id": 001,
"value": "Washington DC"
}]
[{
"id": 002,
"value": "Canberra"
}]
}]
[{
"journeyId": 1002,
"Number": "001",
"DriverName": "Tom",
"Transporter": {
"id": "T2",
"number": "AN01002",
"Company": "Trax"
},
"place": [{
"id": 002,
"value": "Canberra"
}]
[{
"id": 004,
"value": "Vienna"
}]
}]
[{
"journeyId": 1003,
"Number": "004",
"DriverName": "Jack",
"Transporter": {
"id": "T3",
"number": "AN01003",
"Company": "Trax"
},
"place": [{
"id": 001,
"value": "Washington DC"
}]
[{
"id": 004,
"value": "Vienna"
}]
}],
"20180211": [{
"journeyId": 1004,
"Number": "005",
"DriverName": "Jack",
"Transporter": {
"id": "T3",
"number": "AN01013",
"Company": "Trax"
},
"place": [{
"id": 005,
"value": "Bridgetown"
}]
[{
"id": 006,
"value": "Ottawa"
}]
[{
"id": 004,
"value": "Vienna"
}]

}]
[{
"journeyId": 1005,
"Number": "005",
"DriverName": "Jerry",
"Transporter": {
"id": "T3",
"number": "AN01020",
"Company": "Trax"
},
"place": [{
"id": 005,
"value": "Bridgetown"
}]
[{
"id": 006,
"value": "Ottawa"
}]
}],
"20180301": [{
"journeyId": 1006,
"Number": "005",
"DriverName": "demy",
"Transporter": {
"id": "T3",
"number": "AN01003",
"Company": "Trax"
},
"place": [{
"id": 005,
"value": "Bridgetown"
}]
[{
"id": 006,
"value": "Ottawa"
}]
}]};

我正在尝试过滤掉所有具有 place[value]=Vienna 的 trips 键值对。

我的预期输出应该:

trips = {
"20180201":
[{
"journeyId": 1002,
"Number": "001",
"DriverName":"Tom",
"Transporter": {
"id": "T2",
"number": "AN01002",
"Company": "Trax"
}
"place": [{"id":002,"value":"Canberra" }]
[{"id":004,"value":"Vienna"}]
}]
[{
"journeyId": 1003,
"Number": "004",
"DriverName":"Jack",
"Transporter": {
"id": "T3",
"number": "AN01003",
"Company": "Trax"
}
"place": [{"id":001,"value":"Washington DC" }]
[{"id":004,"value":"Vienna"}]
}],
"20180211": [{
"journeyId": 1004,
"Number": "005",
"DriverName":"Jack",
"Transporter": {
"id": "T3",
"number": "AN01013",
"Company": "Trax"
}
"place": [{"id":005,"value":"Bridgetown" }]
[{"id":006,"value":"Ottawa"}]
[{"id":004,"value":"Vienna"}]

}]

};

请帮助我找到正确的方法。我正在尝试以下功能,但卡在中间:

for (var date in trips) {
var res={}
for (var index = 0; index < trips[date].length; index++) {
var data = trips[date][index];

//rest of the logic here

}

}

最佳答案

在这里,您可以使用Array.reduce , Array.filter , Array.someObject.keys

const filteredTrips = Object.keys(trips).reduce((tmp, x) => {
const filtered = trips[x].filter(y => y.place.some(z => z.value === 'Vienna'));

if (filtered.length) {
tmp[x] = filtered;
}

return tmp;
}, {});

const trips = {
"20180201": [{
"journeyId": 1001,
"Number": "001",
"DriverName": "Alex",
"Transporter": {
"id": "T1",
"number": "AN01001",
"Company": "Tranzient"
},
"place": [{
"id": 001,
"value": "Washington DC"
},
{
"id": 002,
"value": "Canberra"
}
],
},
{
"journeyId": 1002,
"Number": "001",
"DriverName": "Tom",
"Transporter": {
"id": "T2",
"number": "AN01002",
"Company": "Trax"
},
"place": [{
"id": 2,
"value": "Canberra"
},
{
"id": 4,
"value": "Vienna"
}
],
},
{
"journeyId": 1003,
"Number": "004",
"DriverName": "Jack",
"Transporter": {
"id": "T3",
"number": "AN01003",
"Company": "Trax"
},
"place": [{
"id": 1,
"value": "Washington DC",
}, {
"id": 4,
"value": "Vienna",
}],
}
],
"20180211": [{
"journeyId": 1004,
"Number": "005",
"DriverName": "Jack",
"Transporter": {
"id": "T3",
"number": "AN01013",
"Company": "Trax"
},
"place": [{
"id": 5,
"value": "Bridgetown"
},
{
"id": 6,
"value": "Ottawa"
},
{
"id": 4,
"value": "Vienna"
}
],

},
{
"journeyId": 1005,
"Number": "005",
"DriverName": "Jerry",
"Transporter": {
"id": "T3",
"number": "AN01020",
"Company": "Trax"
},
"place": [{
"id": 5,
"value": "Bridgetown"
},
{
"id": 6,
"value": "Ottawa"
}
],
}
],
"20180301": [{
"journeyId": 1006,
"Number": "005",
"DriverName": "demy",
"Transporter": {
"id": "T3",
"number": "AN01003",
"Company": "Trax"
},
"place": [{
"id": 5,
"value": "Bridgetown"
},
{
"id": 6,
"value": "Ottawa"
}
],
}],
};

const filteredTrips = Object.keys(trips).reduce((tmp, x) => {
const filtered = trips[x].filter(y => y.place.some(z => z.value === 'Vienna'));

if (filtered.length) {
tmp[x] = filtered;
}

return tmp;
}, {});

console.log(filteredTrips);

关于javascript - 根据提供的值过滤嵌套键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49321217/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com