我第一次做键值对映射而无法接近。我有一个键值对,如:
trips= {
date1: [
{
"id": 1,
"Place": "Delhi",
"Number": "001",
"Vehicle": {"id":"veh1", "number": "AN01001"}
},
{
"id": 2,
"Place": "Bangalore",
"Number": "002",
"Vehicle": {"id":"veh2", "number": "AN01002"}
},
{
"id": 3,
"Place": "Pune",
"Number": "003",
"Vehicle": {"id":"veh3", "number": "AN01003"}
}
],
date2: [
{
"id": 1,
"Place": "Lucknow",
"Number": "001",
"Vehicle": {"id":"veh1", "number": "AN01002"}
},
{
"id": 3,
"Place": "Pune",
"Number": "003",
"Vehicle": {"id":"veh3", "number": "AN01003"}
}
],
date3: [
{
"id": 1,
"Place": "Delhi",
"Number": "001",
"Vehicle": {"id":"veh1", "number": "AN01001"}
},
{
"id": 2,
"Place": "Bangalore",
"Number": "002",
"Vehicle": {"id":"veh2", "number": "AN01002"}
}
]
}
for (date in trips) {
var places = trips[date]
for (var i = 0; i < places.length; ++i) {
var place = places[i]
console.log('place', place)
console.log('Vehicle', place.Vehicle)
}
}
内部日期数据以具有键值对的数组形式存储。我需要打印所有车辆ID为“veh2”的日期。我试图循环数据。但是在阵列开始的某个点之后找不到正确的方法。
我已经能够遍历一个嵌套的键值对
for (key in trips){
var value= trips[key]
for (k in value)
{
//further nested logic
}
}
答案 0 :(得分:0)
我认为你的主要问题是错误的结构化数据,正如@Nina Scholz已经提到的那样,当纠正这个问题时,很容易贯穿所有内容:
trips= {
date1: [
{
"id": 1,
"Place": "Delhi",
"Number": "001",
"Vehicle": {"id":"veh1", "number": "AN01001"}
},
{
"id": 2,
"Place": "Bangalore",
"Number": "002",
"Vehicle": {"id":"veh2", "number": "AN01002"}
},
{
"id": 3,
"Place": "Pune",
"Number": "003",
"Vehicle": {"id":"veh3", "number": "AN01003"}
}
],
date2: [
{
"id": 1,
"Place": "Lucknow",
"Number": "001",
"Vehicle": {"id":"veh1", "number": "AN01002"}
},
{
"id": 3,
"Place": "Pune",
"Number": "003",
"Vehicle": {"id":"veh3", "number": "AN01003"}
}
],
date3: [
{
"id": 1,
"Place": "Delhi",
"Number": "001",
"Vehicle": {"id":"veh1", "number": "AN01001"}
},
{
"id": 2,
"Place": "Bangalore",
"Number": "002",
"Vehicle": {"id":"veh2", "number": "AN01002"}
}
]
}
for (date in trips) {
var places = trips[date]
for (var i = 0; i < places.length; ++i) {
var place = places[i]
console.log('place', place)
console.log('Vehicle', place.Vehicle)
}
}
答案 1 :(得分:0)
使用poper格式化的对象和数组,您可以过滤id匹配的单个位置。
var trips = { date1: [{ id: 1, Place: "Delhi", Number: "001", Vehicle: { id: "veh1", number: "AN01001" } }, { id: 2, Place: "Bangalore", Number: "002", Vehicle: { id: "veh2", number: "AN01002" } }, { id: 3, Place: "Pune", Number: "003", Vehicle: { id: "veh3", number: "AN01003" } }], date2: [{ id: 1, Place: "Lucknow", Number: "001", Vehicle: { id: "veh1", number: "AN01002" } }, { id: 3, Place: "Pune", Number: "003", Vehicle: { id: "veh3", number: "AN01003" } }], date3: [{ id: 1, Place: "Delhi", Number: "001", Vehicle: { id: "veh1", number: "AN01001" } }, { id: 2, Place: "Bangalore", Number: "002", Vehicle: { id: "veh2", number: "AN01002" } }] },
id = "veh2",
result = Object.keys(trips).reduce(function (r, k) {
return r.concat(trips[k].filter(function (place) {
return place.Vehicle.id === id;
}));
}, []);
console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
答案 2 :(得分:0)
您可以使用以下代码执行此操作。
null
&#13;
答案 3 :(得分:0)
以下是获取包含您的车辆的所有日期的一种方法,ID === veh2:
const t = Object.entries(trips);
const res = t.filter((dates) => {
return dates[1].some((d, e) => {
return d.Vehicle.id == 'veh2';
});
});
console.log(res);
这将返回date1和date3(包含veh2)的数组
编辑(版本2):
这会使日期更清晰:
let res = [];
for(let i in trips) {
const found = trips[i].filter((dates) => dates.Vehicle.id == 'veh2');
found.length && res.push(trips[i]);
}
console.log(res);
答案 4 :(得分:-3)
您可以像这样使用Heap
:
Static Area
});