简体   繁体   中英

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.

customobject[0].id
customObject[0].value[]

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.

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.

You should look at some : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some#Polyfill

Way faster than the other methods like filter , map or sort as it was designed for this.

 //List to work against var arr = []; //Populate while (arr.length < 9999999) { arr.push(Math.random() * 999999) } //Set element to find arr[10] = "test"; //Begin timing var d = new Date().getTime(); //Run filter console.log(arr.filter(function(a){return a === "test"}).length > 0); //How long did it take console.log("`filter` took:",new Date().getTime() - d,"ms") //Begin timing d = new Date().getTime(); //Run filter console.log(arr.some(function(a){return a === "test"})); //How long did it take console.log("`some` took:",new Date().getTime() - d,"ms")
 <script> // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some#Polyfill // Production steps of ECMA-262, Edition 5, 15.4.4.17 // Reference: http://es5.github.io/#x15.4.4.17 if (!Array.prototype.some) { Array.prototype.some = function(fun/*, thisArg*/) { 'use strict'; if (this == null) { throw new TypeError('Array.prototype.some called on null or undefined'); } if (typeof fun !== 'function') { throw new TypeError(); } var t = Object(this); var len = t.length >>> 0; var thisArg = arguments.length >= 2 ? arguments[1] : void 0; for (var i = 0; i < len; i++) { if (i in t && fun.call(thisArg, t[i], i, t)) { return true; } } return false; }; } </script>

If your goal is to find the first object in the list that contains 5 in the Value property, then you're looking for Array#find and Array#indexOf :

var gettedcustomObject = customList.find(function(entry) {
    return entry.Value.indexOf(5) != -1;
});

Note that Array#find was added relatively recently and so you may need a polyfill for it (which is trivial). MDN has one .

Or you could use Array#includes instead of indexOf , which will be in ES2017 and is also polyfillable:

var gettedcustomObject = customList.find(function(entry) {
    return entry.Value.includes(5);
});

Live Example (using indexOf ):

 var customList = [ { note: "I'm not a match", Value: [2, 3, 4] }, { note: "I'm not a match either", Value: [78, 4, 27] }, { note: "I'm a match", Value: [889, 5, 27] }, { note: "I'm also a match, but you won't find me", Value: [4, 6, 5] } ]; var gettedcustomObject = customList.find(function(entry) { return entry.Value.indexOf(5) != -1; }); console.log(gettedcustomObject);

If your logic matching the item inside Value were more complicated, you'd use Array#some and a callback function rathe than indexOf . But when looking to see if an array for an entry in an array based on === , indexOf or the new Array#includes are the way to go.

one approach using Array.some() and Array.indexOf(). some loop break once the element is found

var gettedcustomObject;

customobject.some(function(obj){
  if(obj.value.indexOf(5) >-1){
    gettedcustomObject = obj;
    return true;
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM