Javascript 角度绑定到过滤计数
我有这个阵列:Javascript 角度绑定到过滤计数,javascript,angularjs,lodash,Javascript,Angularjs,Lodash,我有这个阵列: [ { type: "hhh", items: [ { "name": "EGFR", "type": "a", "selected": true } ] }, { type: "aaa", items: [ { "name": "mm", "type
[
{
type: "hhh",
items: [
{
"name": "EGFR",
"type": "a",
"selected": true
}
]
},
{
type: "aaa",
items: [
{
"name": "mm",
"type": "b",
"selected": false
}
]
},
{
type: "ii",
items: [
{
"name": "pp",
"type": "bb",
"selected": true
}
]
}
]
我想显示所选属性为true的项目计数器。
我希望它能在变化时实时变化。
没有手表和功能
塞纳克斯 方法如下:
var current_selected = {
get amount(){
var res = 0;
arr.forEach(function(item, i, arr) {
if (item.items[0].selected) res++;
})
return res;
}
}
电话:
current_selected.amount
您可以使用JsonPath获取计数。此外,使用JsonPath还具有处理复杂json结构的额外优势。对于您给出的示例,您只需要包含jsonpath js文件,并在脚本中使用以下内容:
console.log(arr);
var filtered = jsonPath(arr, "$.[*].items[?(@.selected==true)]");
console.log(filtered);
console.log(filtered.length);
其中,arr是json结构
JsonPath可以从以下位置获得:
JsonPath帮助:
其他来源中可能有更新版本,但那是我曾经处理过的版本如何更改阵列?通过任何后端调用/Ajaz/API等?