我有这个JSON:
CLASSPATH
我希望使用特定的{
"1": {
"PerId":"10900662",
"Name":"Cueball",
"Email":"cueb@example.com",
"DepartId":"11"
},
"2": {
"PerId":"10900664",
"Name":"Megan",
"MuEmail":"megan@example.com",
"DepartId":"11"
},
"3": {
"PerId":"10900665",
"Name":"Beret Guy",
"MuEmail":"bg@example.com",
"DepartId":"12"
}
}
这是我测试过的代码,来自this Stack Overflow question:
DepartId
在这种情况下,y返回一个对象,但Firefox抛出<html>
<body>
Test!
</body>
<script>
var y = JSON.parse('{"1":{"PerId":"10900662","Name":"Cueball","Email":"cueb@example.com","DepartId":"11"},"2": {"PerId":"10900664","Name":"Megan","MuEmail":"megan@example.com","DepartId":"11"},"3": {"PerId":"10900665","Name":"Beret Guy","MuEmail":"bg@example.com","DepartId":"12"}}');
var z = y.filter(function (i,n){return n.DepartId == '11'})
</script>
</html>
我希望你能像
那样回归y.filter is not a function.
以JavaScript对象的形式。我如何使它工作?
答案 0 :(得分:1)
filter()
在Array
原型上定义。它不能用在对象上。
您可以使用Object.keys()
获取对象和for
循环中的所有键以迭代它们。
// Get all keys from the `obj`
var keys = Object.keys(obj),
result = {};
// Iterate over all properties in obj
for (var i = 0, len = keys.length; i < len; i++) {
// If the ID is to be filtered
if (obj[keys[i]].DepartId === '11') {
// Add the object in the result object
result[keys[i]] = obj[keys[i]];
}
}
var obj = {
"1": {
"PerId": "10900662",
"Name": "Cueball",
"Email": "cueb@example.com",
"DepartId": "11"
},
"2": {
"PerId": "10900664",
"Name": "Megan",
"MuEmail": "megan@example.com",
"DepartId": "11"
},
"3": {
"PerId": "10900665",
"Name": "Beret Guy",
"MuEmail": "bg@example.com",
"DepartId": "12"
}
};
var keys = Object.keys(obj),
result = {};
for (var i = 0, len = keys.length; i < len; i++) {
if (obj[keys[i]].DepartId === '11') {
result[keys[i]] = obj[keys[i]];
}
}
console.log(result);
document.body.innerHTML = '<pre>' + JSON.stringify(result, 0, 4);
&#13;
我建议更改数据格式以使用对象数组。然后filter()
可以直接应用于数组。
var arr = [{
"PerId": "10900662",
"Name": "Cueball",
"Email": "cueb@example.com",
"DepartId": "11"
}, {
"PerId": "10900664",
"Name": "Megan",
"MuEmail": "megan@example.com",
"DepartId": "11"
}, {
"PerId": "10900665",
"Name": "Beret Guy",
"MuEmail": "bg@example.com",
"DepartId": "12"
}];
var result = arr.filter(obj => obj.DepartId === '11');
console.log(result);
var arr = [{
"PerId": "10900662",
"Name": "Cueball",
"Email": "cueb@example.com",
"DepartId": "11"
}, {
"PerId": "10900664",
"Name": "Megan",
"MuEmail": "megan@example.com",
"DepartId": "11"
}, {
"PerId": "10900665",
"Name": "Beret Guy",
"MuEmail": "bg@example.com",
"DepartId": "12"
}];
var result = arr.filter(obj => obj.DepartId === '11');
console.log(result);
document.body.innerHTML = '<pre>' + JSON.stringify(result, 0, 4);
&#13;
答案 1 :(得分:0)
您尝试在对象上使用数组过滤器。再看看这个例子。
$([ // this is an array of objects
{"name":"Lenovo Thinkpad 41A4298","website":"google222"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"}
])
.filter(function (i,n){
return n.website==='google';
});
答案 2 :(得分:0)
filter
是数组函数的原型而不是object
,所以我们不能在这里使用。
要实现输出,我们可以在for-in
object
循环
var y = JSON.parse('{"1":{"PerId":"10900662","Name":"Cueball","Email":"cueb@example.com","DepartId":"11"},"2": {"PerId":"10900664","Name":"Megan","MuEmail":"megan@example.com","DepartId":"11"},"3": {"PerId":"10900665","Name":"Beret Guy","MuEmail":"bg@example.com","DepartId":"12"}}');
var res = {}
for(var key in y){
if(y[key].DepartId === '11')
res[key] = y[key]
}
console.log(res)