Sush Sush - 9 months ago 44
JSON Question

Find items from json array using NodeJS

Am trying to search particular items from a json array and returs the items

result = {
FF: {
server: 'deskes.com',
result: 'succes'

},
{
server: 'cleantarge.com',
result: 'Failed'

},
{
server: 'fance34.com',
result: 'success'

},{
server: 'deskes.com',
result: 'Failed'

},

}


I have following json data.
i want to search items based on server

i used the code

var arrFound = result.filter(function(item) {
return item.server == 'deskes.com';
});

But getting TypeError: Object #<Object> has no method 'filter'


my resulting json wiil be after searching the data is

result = {
FF: {
server: 'deskes.com',
result: 'succes'

},

{
server: 'deskes.com',
result: 'Failed'

}

}

Answer Source

The filter method is only available on arrays, and it looks like you're calling it on an object (from the outer bracket). You can work around this by looping through the keys:

var arrFound = Object.keys(result).filter(function(key) {
    return result[key].server == 'deskes.com';
// to cast back from an array of keys to the object, with just the passing ones
}).reduce(function(obj, key){
    obj[key] = result[key];
    return obj;
}, {});;