我有一个数组
vendors = [{
Name: 'Magenic',
ID: 'ABC'
},
{
Name: 'Microsoft',
ID: 'DEF'
} // and so on...
];
我如何检查这个数组,看看“Magenic”是否存在?我不想循环,除非迫不得已。我可能要处理几千条记录。
我有一个数组
vendors = [{
Name: 'Magenic',
ID: 'ABC'
},
{
Name: 'Microsoft',
ID: 'DEF'
} // and so on...
];
我如何检查这个数组,看看“Magenic”是否存在?我不想循环,除非迫不得已。我可能要处理几千条记录。
当前回答
const a = [{one:2},{two:2},{two:4}]
const b = a.filter(val => "two" in val).length;
if (b) {
...
}
其他回答
接受的答案仍然有效,但现在我们有一个ECMAScript 6本机方法[数组。find][1]和[数组。有些][2]来达到同样的效果。
Array.some
如果你只想确定一个元素是否存在,也就是说,你需要一个真/假的判断。
引用中数:
some()方法测试数组中是否至少有一个元素通过了所提供函数实现的测试。如果在数组中,它找到了提供的函数返回true的元素,则返回true;否则返回false。它不会修改数组。
Array.find
如果你想从数组else中获得匹配的对象,则使用find返回undefined。
引用中数:
find()方法返回所提供数组中满足所提供测试函数的第一个元素的值。如果没有满足测试函数的值,则返回undefined。
var arr = [
{
id: 21,
label: 'Banana',
},
{
id: 22,
label: 'Apple',
}
]
/* note : data is the actual object that matched search criteria
or undefined if nothing matched */
var data = arr.find(function(ele) {
return ele.id === 21;
});
if (data) {
console.log('found');
console.log(data); // This is entire object i.e. `item` not boolean
}
/* note : doesExist is a boolean thats true or false depending on of whether the data was found or not */
var doesExist = arr.some(function(ele) {
return ele.id === 21;
});
有一个由mozilla提供的IE的polyfill
我是这么做的
const found = vendors.some(item => item.Name === 'Magenic');
array.some()方法检查数组中是否至少有一个值符合条件,并返回一个布尔值。 从这里开始,你可以选择:
if (found) {
// do something
} else {
// do something else
}
数组元素测试:
JS提供了数组函数,允许你相对容易地实现这一点。它们是:
array .prototype.filter:接受一个回调函数,这个回调函数是一个测试,然后数组被迭代,并根据这个回调进行过滤。返回一个新的过滤数组。 array .prototype.some:接受一个回调函数,它是一个测试,然后使用is callback迭代数组,如果任何元素通过测试,则返回布尔值true。否则返回false
具体细节最好通过一个例子来解释:
例子:
vendors = [ { Name: 'Magenic', ID: 'ABC' }, { Name: 'Microsoft', ID: 'DEF' } //and so on goes array... ]; // filter returns a new array, we instantly check if the length // is longer than zero of this newly created array if (vendors.filter(company => company.Name === 'Magenic').length ) { console.log('I contain Magenic'); } // some would be a better option then filter since it directly returns a boolean if (vendors.some(company => company.Name === 'Magenic')) { console.log('I also contain Magenic'); }
浏览器支持:
这两个函数都是ES6函数,不是所有浏览器都支持。为了克服这个问题,你可以使用填充剂。下面是Array.prototype.some(来自MDN)的填充:
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; for (var i = 0; i < len; i++) { if (i in t && fun.call(thisArg, t[i], i, t)) { return true; } } return false; }; }
const a = [{one:2},{two:2},{two:4}]
const b = a.filter(val => "two" in val).length;
if (b) {
...
}
我宁愿用正则表达式。
如果您的代码如下所示,
vendors = [
{
Name: 'Magenic',
ID: 'ABC'
},
{
Name: 'Microsoft',
ID: 'DEF'
}
];
我推荐
/"Name":"Magenic"/.test(JSON.stringify(vendors))