我需要在javascript对象中找到各种数组中的项目。数组位于嵌套的javascript对象中。我已经实现了一种获取我需要的数据的方法,但它看起来很长,有4个嵌套for循环。
我意识到我可以使用for循环来迭代元素,但这不是最漂亮的方式。我已经研究了如何使用过滤器函数来访问我感兴趣的顶级项目,但是很难弄清楚如何过滤内部项目。
在我的下面的代码中,interesting_lines得到了适当的填充。下一步是创建一个所有元素的列表,其中包含" fw"的measure.value.label。然后打印measurement.value.value数字。我怎么能这样做?
// lines crossed that we are interested in
let interest = ["Entry", "Exit"];
let lines = {"element":[{"element-id":2,"element-name":"line_2.1","sensor-type":"MULTI_SENSOR","data-type":"LINE","from":"2018-05-08T08:59:00Z","to":"2018-05-08T09:00:00Z","resolution":"ONE_MINUTE","measurement":[{"from":"2018-05-08T08:59:00Z","to":"2018-05-08T09:00:00Z","value":[{"value":0,"label":"fw"},{"value":0,"label":"bw"}]}]},{"element-id":3,"element-name":"line_2.2","sensor-type":"MULTI_SENSOR","data-type":"LINE","from":"2018-05-08T08:59:00Z","to":"2018-05-08T09:00:00Z","resolution":"ONE_MINUTE","measurement":[{"from":"2018-05-08T08:59:00Z","to":"2018-05-08T09:00:00Z","value":[{"value":1,"label":"fw"},{"value":0,"label":"bw"}]}]},{"element-id":1,"element-name":"Entry","sensor-type":"MULTI_SENSOR","data-type":"LINE","from":"2018-05-08T08:59:00Z","to":"2018-05-08T09:00:00Z","resolution":"ONE_MINUTE","measurement":[{"from":"2018-05-08T08:59:00Z","to":"2018-05-08T09:00:00Z","value":[{"value":1,"label":"fw"},{"value":0,"label":"bw"}]}]},{"element-id":0,"element-name":"Exit","sensor-type":"MULTI_SENSOR","data-type":"LINE","from":"2018-05-08T08:59:00Z","to":"2018-05-08T09:00:00Z","resolution":"ONE_MINUTE","measurement":[{"from":"2018-05-08T08:59:00Z","to":"2018-05-08T09:00:00Z","value":[{"value":0,"label":"fw"},{"value":0,"label":"bw"}]}]},{"element-id":4,"element-name":"line_2.3","sensor-type":"MULTI_SENSOR","data-type":"LINE","from":"2018-05-08T08:59:00Z","to":"2018-05-08T09:00:00Z","resolution":"ONE_MINUTE","measurement":[{"from":"2018-05-08T08:59:00Z","to":"2018-05-08T09:00:00Z","value":[{"value":0,"label":"fw"},{"value":0,"label":"bw"}]}]},{"element-id":5,"element-name":"line_2.4","sensor-type":"MULTI_SENSOR","data-type":"LINE","from":"2018-05-08T08:59:00Z","to":"2018-05-08T09:00:00Z","resolution":"ONE_MINUTE","measurement":[{"from":"2018-05-08T08:59:00Z","to":"2018-05-08T09:00:00Z","value":[{"value":0,"label":"fw"},{"value":0,"label":"bw"}]}]}]};
console.log(`lines in collection=${lines.element.length}`); // 6
// filter function on line name
function required_line(item) {
for(var i = 0; i < interest.length; ++i) {
if (item["element-name"] == interest[i]) {
return true;
}
}
return false;
}
const interesting_lines = lines.element.filter(required_line);
console.log(`result of filter: ${JSON.stringify(interesting_lines)} `);
// filter out by label - eg fwd???
var morefiltered=interesting_lines.filter( function(element) { return element["measurement"]["value"]["label"] == "fw"; });
// above line fails - TypeError: Cannot read property 'label' of undefined
console.log(`2nd level filter: ${JSON.stringify(morefiltered)} `);
我的预期输出是:
forward line crossing numbers:
line name: Entry, value: 1
line name: Exit, value: 0
答案 0 :(得分:0)
由于measurement
和value
是数组,因此您必须迭代它们,
const morefiltered=interesting_lines.filter( element => {
return element.measurement.filter(a => {
return a.value.filter(b => {
return b.label == "fw";
})
})
});
并创建一个空数组并用匹配的值填充它:
// lines crossed that we are interested in
let interest = ["Entry", "Exit"];
let lines = {
"element": [{
"element-id": 2,
"element-name": "line_2.1",
"sensor-type": "MULTI_SENSOR",
"data-type": "LINE",
"from": "2018-05-08T08:59:00Z",
"to": "2018-05-08T09:00:00Z",
"resolution": "ONE_MINUTE",
"measurement": [{
"from": "2018-05-08T08:59:00Z",
"to": "2018-05-08T09:00:00Z",
"value": [{
"value": 0,
"label": "fw"
}, {
"value": 0,
"label": "bw"
}]
}]
}, {
"element-id": 3,
"element-name": "line_2.2",
"sensor-type": "MULTI_SENSOR",
"data-type": "LINE",
"from": "2018-05-08T08:59:00Z",
"to": "2018-05-08T09:00:00Z",
"resolution": "ONE_MINUTE",
"measurement": [{
"from": "2018-05-08T08:59:00Z",
"to": "2018-05-08T09:00:00Z",
"value": [{
"value": 1,
"label": "fw"
}, {
"value": 0,
"label": "bw"
}]
}]
}, {
"element-id": 1,
"element-name": "Entry",
"sensor-type": "MULTI_SENSOR",
"data-type": "LINE",
"from": "2018-05-08T08:59:00Z",
"to": "2018-05-08T09:00:00Z",
"resolution": "ONE_MINUTE",
"measurement": [{
"from": "2018-05-08T08:59:00Z",
"to": "2018-05-08T09:00:00Z",
"value": [{
"value": 1,
"label": "fw"
}, {
"value": 0,
"label": "bw"
}]
}]
}, {
"element-id": 0,
"element-name": "Exit",
"sensor-type": "MULTI_SENSOR",
"data-type": "LINE",
"from": "2018-05-08T08:59:00Z",
"to": "2018-05-08T09:00:00Z",
"resolution": "ONE_MINUTE",
"measurement": [{
"from": "2018-05-08T08:59:00Z",
"to": "2018-05-08T09:00:00Z",
"value": [{
"value": 0,
"label": "fw"
}, {
"value": 0,
"label": "bw"
}]
}]
}, {
"element-id": 4,
"element-name": "line_2.3",
"sensor-type": "MULTI_SENSOR",
"data-type": "LINE",
"from": "2018-05-08T08:59:00Z",
"to": "2018-05-08T09:00:00Z",
"resolution": "ONE_MINUTE",
"measurement": [{
"from": "2018-05-08T08:59:00Z",
"to": "2018-05-08T09:00:00Z",
"value": [{
"value": 0,
"label": "fw"
}, {
"value": 0,
"label": "bw"
}]
}]
}, {
"element-id": 5,
"element-name": "line_2.4",
"sensor-type": "MULTI_SENSOR",
"data-type": "LINE",
"from": "2018-05-08T08:59:00Z",
"to": "2018-05-08T09:00:00Z",
"resolution": "ONE_MINUTE",
"measurement": [{
"from": "2018-05-08T08:59:00Z",
"to": "2018-05-08T09:00:00Z",
"value": [{
"value": 0,
"label": "fw"
}, {
"value": 0,
"label": "bw"
}]
}]
}]
};
// console.log(`lines in collection=${lines.element.length}`); // 6
// filter function on line name
function required_line(item) {
for (var i = 0; i < interest.length; ++i) {
if (item["element-name"] == interest[i]) {
return true;
}
}
return false;
}
const interesting_lines = lines.element.filter(required_line);
//console.log(`result of filter: ${JSON.stringify(interesting_lines)} `);
let values = [];
// filter out by label - eg fwd >>>
const morefiltered = interesting_lines.filter(element => {
return element.measurement.filter(a => {
return a.value.filter(b => {
if(b.label == "fw"){
values.push({name : element["element-name"], value : b.value});
return true;
}
})
})
});
console.log('values : ', JSON.stringify(values))
console.log(`2nd level filter: ${JSON.stringify(morefiltered)} `);
&#13;
答案 1 :(得分:0)
数据结构不是很好,所以如果你可以改变它,我会建议,这反过来会使你的算法更简单。
我尝试使用underscoreJS为您解决问题,但我不完全确定这是您想要的。
var myObjects = _.filter(lines.element, function(l){
return _.any(interest, function(i){
return l["element-name"] == i;
});
});