我有一个这样的数组:

[{prop1:"abc",prop2:"qwe"},{prop1:"bnmb",prop2:"yutu"},{prop1:"zxvz",prop2:"qwrq"},...]

我怎么能得到对象的索引,匹配的条件,而不是在整个数组上迭代?

例如,给定prop2=="yutu",我想获得索引1。

我看到过. indexof(),但认为它用于简单的数组,如["a1","a2",…]。我还检查了$.grep(),但这返回对象,而不是索引。


当前回答

从2016年开始,你应该使用Array。findIndex (ES2015/ES6标准)用于:

a = [ {prop1 " abc " prop2 qwe "} ", {prop1 bnmb "、" prop2 yutu "}, " {prop1 zxvz "、" prop2 qwrq "} "]; index = a.f ndindex (x => x.p rp2 ==="yutu"); console.log (index);

支持谷歌Chrome, Firefox和Edge。对于ie浏览器,在链接页面上有一个填充。

性能报告

函数调用是非常昂贵的,因此对于非常大的数组,一个简单的循环将比findIndex执行得更好:

let test = []; for (let i = 0; i < 1e6; i++) test.push({prop: i}); let search = test.length - 1; let count = 100; console.time('findIndex/predefined function'); let fn = obj => obj.prop === search; for (let i = 0; i < count; i++) test.findIndex(fn); console.timeEnd('findIndex/predefined function'); console.time('findIndex/dynamic function'); for (let i = 0; i < count; i++) test.findIndex(obj => obj.prop === search); console.timeEnd('findIndex/dynamic function'); console.time('loop'); for (let i = 0; i < count; i++) { for (let index = 0; index < test.length; index++) { if (test[index].prop === search) { break; } } } console.timeEnd('loop');

与大多数优化一样,这应该在实际需要时谨慎应用。

其他回答

从2016年开始,你应该使用Array。findIndex (ES2015/ES6标准)用于:

a = [ {prop1 " abc " prop2 qwe "} ", {prop1 bnmb "、" prop2 yutu "}, " {prop1 zxvz "、" prop2 qwrq "} "]; index = a.f ndindex (x => x.p rp2 ==="yutu"); console.log (index);

支持谷歌Chrome, Firefox和Edge。对于ie浏览器,在链接页面上有一个填充。

性能报告

函数调用是非常昂贵的,因此对于非常大的数组,一个简单的循环将比findIndex执行得更好:

let test = []; for (let i = 0; i < 1e6; i++) test.push({prop: i}); let search = test.length - 1; let count = 100; console.time('findIndex/predefined function'); let fn = obj => obj.prop === search; for (let i = 0; i < count; i++) test.findIndex(fn); console.timeEnd('findIndex/predefined function'); console.time('findIndex/dynamic function'); for (let i = 0; i < count; i++) test.findIndex(obj => obj.prop === search); console.timeEnd('findIndex/dynamic function'); console.time('loop'); for (let i = 0; i < count; i++) { for (let index = 0; index < test.length; index++) { if (test[index].prop === search) { break; } } } console.timeEnd('loop');

与大多数优化一样,这应该在实际需要时谨慎应用。

为什么不进行迭代呢?新的Array.prototype.forEach非常适合这个目的!

如果需要,可以使用二叉搜索树通过单个方法调用进行查找。这是一个整洁的实现的BTree和红黑搜索树在JS - https://github.com/vadimg/js_bintrees -但我不确定你是否能同时找到索引。

最好和最快的方法是:

const products = [
  { prop1: 'telephone', prop2: 996 },
  { prop1: 'computadora', prop2: 1999 },
  { prop1: 'bicicleta', prop2: 995 },
];

const index = products.findIndex(el => el.prop2 > 1000);

console.log(index); // 1

另一种简单的方法是:

 function getIndex(items) {
        for (const [index, item] of items.entries()) {
            if (item.prop2 === 'yutu') {
                return index;
            }
        }
    }

const myIndex = getIndex(myArray);

我在上面看到了很多解决方案。

在这里,我使用map函数在数组对象中查找搜索文本的索引。

我将用学生数据来解释我的答案。

步骤1:为学生创建数组对象(可选,您可以创建自己的数组对象)。 学生var =[{名称:“Rambabu htno: " 1245 "},{名称:“迪”,htno: " 1246 "},{名称:“poojitha htno: " 1247 "},{名称:“magitha htno: " 1248 "}); 步骤2:创建变量搜索文本 var studentNameToSearch =“Divya”; 步骤3:创建变量来存储匹配的索引(这里我们使用map函数进行迭代)。 var matchedIndex = students。Map(函数(obj){返回obj.name;}) .indexOf (studentNameToSearch);

学生var =[{名称:“Rambabu htno: " 1245 "},{名称:“迪”,htno: " 1246 "},{名称:“poojitha htno: " 1247 "},{名称:“magitha htno: " 1248 "}); var studentNameToSearch =“Divya”; var matchedIndex = students。Map(函数(obj){返回obj.name;}) .indexOf (studentNameToSearch); console.log (matchedIndex); alert("你的搜索名称索引在数组中是:"+matchedIndex)