[英]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.如果值存在于列表中,我正在使用 grep 和 map 函数来获取对象,但它不能很好地工作。
I have a list customList and the customObject has the int id property and List value properties.我有一个列表 customList 并且 customObject 具有 int id 属性和 List value 属性。
customobject[0].id
customObject[0].value[]
What I want is check if in the List the value 5 exists.我想要的是检查列表中是否存在值 5。
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.注意:2x foreach 可能是一个解决方案,但 customList 有超过 1000 个对象,具有 10000 个值。 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.如果v.Value
是一个数组,这将起作用。
You should look at some
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some#Polyfill你应该看看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.比为此设计的其他方法(如filter
、 map
或sort
快得多。
//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
:如果您的目标是在Value
属性中找到包含5
的列表中的第一个对象,那么您正在寻找Array#find
和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).请注意, Array#find
是最近添加的,因此您可能需要一个 polyfill(这很简单)。 MDN has one . MDN 有一个.
Or you could use Array#includes
instead of indexOf
, which will be in ES2017 and is also polyfillable:或者你可以使用Array#includes
而不是indexOf
,它将在 ES2017 中并且也是可填充的:
var gettedcustomObject = customList.find(function(entry) {
return entry.Value.includes(5);
});
Live Example (using indexOf
):实时示例(使用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
.如果与Value
中的项目匹配的逻辑更复杂,那么您将使用Array#some
和回调函数而不是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.但是,当查看基于===
、 indexOf
或新的Array#includes
的数组中条目的数组是否是要走的路。
one approach using Array.some() and Array.indexOf().一种使用 Array.some() 和 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;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.