簡體   English   中英

如果列表返回對象<object>值存在(Javascript,jquery grep/map)

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

如果值存在於列表中,我正在使用 grep 和 map 函數來獲取對象,但它不能很好地工作。

我有一個列表 customList 並且 customObject 具有 int id 屬性和 List value 屬性。

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

我想要的是檢查列表中是否存在值 5。

我正在使用的功能是:

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

我做錯了什么,正確的實現是什么?

注意:2x foreach 可能是一個解決方案,但 customList 有超過 1000 個對象,具有 10000 個值。 我認為這會減慢進程。

這應該這樣做。

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

如果v.Value是一個數組,這將起作用。

你應該看看somehttps : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some#Polyfill

比為此設計的其他方法(如filtermapsort快得多。

 //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>

如果您的目標是在Value屬性中找到包含5的列表中的第一個對象,那么您正在尋找Array#findArray#indexOf

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

請注意, Array#find是最近添加的,因此您可能需要一個 polyfill(這很簡單)。 MDN 有一個.

或者你可以使用Array#includes而不是indexOf ,它將在 ES2017 中並且也是可填充的:

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

實時示例(使用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);

如果與Value中的項目匹配的邏輯更復雜,那么您將使用Array#some和回調函數而不是indexOf 但是,當查看基於===indexOf或新的Array#includes的數組中條目的數組是否是要走的路。

一種使用 Array.some() 和 Array.indexOf() 的方法。 一旦找到元素,一些循環就會中斷

var gettedcustomObject;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM