最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - can I use array.indexOf() with a function like .map() - Stack Overflow

programmeradmin1浏览0评论

I am using Extendscript which uses ECMAScript 3. So I am locked to many limitations. I am looking to find something similar to Arr.find(func) but available for ECMA3. Because I need a method searchs array of objects and find one object that has specific value in one of his properties. So I found Arr.indexOf() which is ECMA3 but not sure how to use it with a function since this method works with array of strings.

My question is is there a way to use .indexOf() with function (like .find()) to make it work with objects or any other solution?

Thanks,

I am using Extendscript which uses ECMAScript 3. So I am locked to many limitations. I am looking to find something similar to Arr.find(func) but available for ECMA3. Because I need a method searchs array of objects and find one object that has specific value in one of his properties. So I found Arr.indexOf() which is ECMA3 but not sure how to use it with a function since this method works with array of strings.

My question is is there a way to use .indexOf() with function (like .find()) to make it work with objects or any other solution?

Thanks,

Share asked Mar 2, 2018 at 19:20 HTN3DHTN3D 131 gold badge1 silver badge2 bronze badges 9
  • You should post the code you are working with. It is unclear the exact structure that you need to search through. – Scott Marcus Commented Mar 2, 2018 at 19:22
  • 1 I'm pretty sure indexOf Is ECMAScript 5.1; are you sure you can use it? Either way, the answer to your question is no, indexOf does not work with a function like find. You could possibly use map to convert the values of your array into something that would work with indexOf or use filter to filter your array down to just the values you're trying to find and then select the first of those – Hamms Commented Mar 2, 2018 at 19:25
  • 2 I don't see array.indexOf in the ECMA 3 Spec (although string.indexOf is indeed there) – Hamms Commented Mar 2, 2018 at 19:36
  • 1 @Hamms My mistake. Comment deleted. – Scott Marcus Commented Mar 2, 2018 at 19:36
  • 1 @HTN3D - have you considered polyfilling the find() method? Although that isn't the answer to your question, I do believe it is your solution. – Randy Casburn Commented Mar 2, 2018 at 19:45
 |  Show 4 more ments

3 Answers 3

Reset to default 2

.indexOf() won't help you here because it only searches arrays. You need to search objects inside the array, so you'll need to loop over the objects and attempt to find matches manually.

var objArray = [
  {key1: "foo", key2: true, key3: 10 },
  {key1: "foo2", key2: false, key3: 100 },  
  {key1: "foo3", key2: true, key3: 101, key4: 101 }
];

function findKey(ary, findVal){
  // Loop over the array
  for(var i = 0; i < ary.length; i++){
  
    // Loop over objects keys
    for(var key in ary[i]){
      // Compare current key value against passed value to find
      if(ary[i][key] === findVal){
         console.log(findVal + " was found in object: " + i + ", key: " + key);
      }
    }      
  }
}

findKey(objArray, 101);
findKey(objArray, true);

findIndex is the Array-function that takes a function instead of value but gives index. I'm not familiar with ExtendScript, but there is a polyfill in that Mozilla Developer page that could be added. If there is map and find then findIndex could be there as well, but you could also bine map and indexOf with following way for a one liner.

var people = [{name: 'bob', age: 25}, {name: 'mary', age: 5}]
var index = people.map(p => p.name).indexOf('bob')

Thank you so much for all answers. They were all useful to me. I used this function based on Scott Marcus suggestion:

  function findItem(list){
    for (var i=0; i < list.length; i++){
      if (list[i].property === true){return i}
    }
  }
发布评论

评论列表(0)

  1. 暂无评论