Reputation: 362
I have this JSON:
{
"1": {
"PerId":"10900662",
"Name":"Cueball",
"Email":"[email protected]",
"DepartId":"11"
},
"2": {
"PerId":"10900664",
"Name":"Megan",
"MuEmail":"[email protected]",
"DepartId":"11"
},
"3": {
"PerId":"10900665",
"Name":"Beret Guy",
"MuEmail":"[email protected]",
"DepartId":"12"
}
}
Which I want to filter with a specific DepartId
Here's the code I've tested, which is from this Stack Overflow question:
<html>
<body>
Test!
</body>
<script>
var y = JSON.parse('{"1":{"PerId":"10900662","Name":"Cueball","Email":"[email protected]","DepartId":"11"},"2": {"PerId":"10900664","Name":"Megan","MuEmail":"[email protected]","DepartId":"11"},"3": {"PerId":"10900665","Name":"Beret Guy","MuEmail":"[email protected]","DepartId":"12"}}');
var z = y.filter(function (i,n){return n.DepartId == '11'})
</script>
</html>
In this case, y returns an object, but Firefox throws error that y.filter is not a function.
I expected y to return as something like
{
"1": {
"PerId":"10900662",
"Name":"Cueball",
"Email":"[email protected]",
"DepartId":"11"
},
"2": {
"PerId":"10900664",
"Name":"Megan",
"MuEmail":"[email protected]",
"DepartId":"11"
}
}
in the form of JavaScript object. How do I make it work?
Upvotes: 2
Views: 122
Reputation: 87203
filter()
is defined on the Array
prototype. It cannot be used on object.
You can use Object.keys()
to get all the keys in the object and for
loop to iterate over them.
// 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": "[email protected]",
"DepartId": "11"
},
"2": {
"PerId": "10900664",
"Name": "Megan",
"MuEmail": "[email protected]",
"DepartId": "11"
},
"3": {
"PerId": "10900665",
"Name": "Beret Guy",
"MuEmail": "[email protected]",
"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);
I'll recommend to change the data format to use array of objects. Then filter()
can be directly applied on array.
var arr = [{
"PerId": "10900662",
"Name": "Cueball",
"Email": "[email protected]",
"DepartId": "11"
}, {
"PerId": "10900664",
"Name": "Megan",
"MuEmail": "[email protected]",
"DepartId": "11"
}, {
"PerId": "10900665",
"Name": "Beret Guy",
"MuEmail": "[email protected]",
"DepartId": "12"
}];
var result = arr.filter(obj => obj.DepartId === '11');
console.log(result);
var arr = [{
"PerId": "10900662",
"Name": "Cueball",
"Email": "[email protected]",
"DepartId": "11"
}, {
"PerId": "10900664",
"Name": "Megan",
"MuEmail": "[email protected]",
"DepartId": "11"
}, {
"PerId": "10900665",
"Name": "Beret Guy",
"MuEmail": "[email protected]",
"DepartId": "12"
}];
var result = arr.filter(obj => obj.DepartId === '11');
console.log(result);
document.body.innerHTML = '<pre>' + JSON.stringify(result, 0, 4);
Upvotes: 1
Reputation: 7742
filter
is prototype of array function not object
,so we can not use here.
To achieve output, we can use for-in
loop on object
var y = JSON.parse('{"1":{"PerId":"10900662","Name":"Cueball","Email":"[email protected]","DepartId":"11"},"2": {"PerId":"10900664","Name":"Megan","MuEmail":"[email protected]","DepartId":"11"},"3": {"PerId":"10900665","Name":"Beret Guy","MuEmail":"[email protected]","DepartId":"12"}}');
var res = {}
for(var key in y){
if(y[key].DepartId === '11')
res[key] = y[key]
}
console.log(res)
Upvotes: 0
Reputation: 13775
You're trying to use an array filter on an object. Look over the example again.
$([ // 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';
});
Upvotes: 0