E75 E75 - 1 year ago 96
Javascript Question

return object if list<object> value is exists (Javascript, jquery grep/map)

I'm using grep and map functions to get the object if the value is exists in the list, but it does not work well.

I have a list customList and the customObject has the int id property and List value properties.


What I want is check if in the List the value 5 exists.

The function what I'm using is:

var gettedcustomObject = $.grep(customList, function (e) {
var result = e.Value.map(function (a) { return a === 5;});
return result;

What am I doing wrong and what is the correct implementation?

Note: 2x foreach could be a solution, but customList has more than 1000 objects with 10000 values. I think that slow down the proces.

Answer Source

This should do it.

var gettedcustomObject = customList.filter(function(v){
    var ln = v.Value.length;
    for(var i = 0; i < ln; i++){
       if(v.Value[i] == 5){
           return true;
    return false;
    // Or simply:
    // return v.Value.indexOf(5) != -1;

This will work if v.Value is an array.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download