我有一个数组,其中的内容从json响应中推送。现在,我需要在不知道生命体名称的情况下检查并检索真实的生命体名称。任何有关如何访问数组元素以找出最重要的事实的想法都会很有帮助
var json_data = {
"Blood Pressure": true,
"Body Composition": true,
"ECG": true,
"SPO2": false,
"Weight Scale": true,
"temp": true
};
var hardwarelist = [];
for (var i in json_data) {
hardwarelist.push([i, json_data[i]]);
}
var vitalflow = ["BP", "Weight", "BMC", "ECG", "Spo2", "Thermometer"];
for (var i = 0; i < vitalflow.length; i++) {
//console.log(vitalflow[i]);
}
function hardwareavailability(hardwarelistarr, vitalflowarr) {
var hardwarelistarray = new Array();
hardwarelistarray = hardwarelistarr;
var flowarray = new Array();
flowarray = vitalflowarr;
if (hardwarelistarray[0]) {
window.location.href = "#/about";
}
}
hardwareavailability(hardwarelist, vitalflow);
答案 0 :(得分:3)
使用filter
获取正确的元素,然后map
列出名称:
var json_data = {
"Blood Pressure": true,
"Body Composition": true,
"ECG": true,
"SPO2": false,
"Weight Scale": true,
"temp": true
};
var hardwarelist = [];
for (var i in json_data) {
hardwarelist.push([i, json_data[i]]);
}
var trueVitals = hardwarelist.filter(([, e]) => e).map(([e]) => e);
console.log(trueVitals);
ES5语法:
var json_data = {
"Blood Pressure": true,
"Body Composition": true,
"ECG": true,
"SPO2": false,
"Weight Scale": true,
"temp": true
};
var hardwarelist = [];
for (var i in json_data) {
hardwarelist.push([i, json_data[i]]);
}
var trueVitals = hardwarelist.filter(function(e) {
return e[1];
}).map(function(e) {
return e[0];
});
console.log(trueVitals);
答案 1 :(得分:0)
使用filter。
var json_data = {
"Blood Pressure": true,
"Body Composition": true,
"ECG": true,
"SPO2": false,
"Weight Scale": true,
"temp": true
};
var hardwarelist = [];
for (var i in json_data) {
hardwarelist.push([i, json_data[i]]);
}
console.log(hardwarelist.filter(h => h[1]))