2

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?

2
  • 1
    filter() can be applied for array not object Commented Apr 26, 2016 at 4:06
  • What you got is an object. Filter only work with arrays, it should be [ { "PerId":"10900662", "Name":"Cueball", "Email":"[email protected]", "DepartId":"11" }, { "PerId":"10900664", "Name":"Megan", "MuEmail":"[email protected]", "DepartId":"11" } ]; Commented Apr 26, 2016 at 4:14

3 Answers 3

1

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);

Sign up to request clarification or add additional context in comments.

Comments

0

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';
    });

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.