Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用特定键的特定值查找数组中对象的索引_Javascript_Arrays_Javascript Objects - Fatal编程技术网

Javascript 使用特定键的特定值查找数组中对象的索引

Javascript 使用特定键的特定值查找数组中对象的索引,javascript,arrays,javascript-objects,Javascript,Arrays,Javascript Objects,我有一个对象,需要在其中查找特定的项目索引号。以下是我的目标: [ { "type": "Grayscale", "mode": "average" }, { "type": "Sepia" }, { "type": "Invert", "invert": true }, { "type": "Convolute", "opaque": false, "matrix": [1, 1, 1, 1, 0.7, -1, -

我有一个对象,需要在其中查找特定的项目索引号。以下是我的目标:

[
  {
    "type": "Grayscale",
    "mode": "average"
  }, {
    "type": "Sepia"
  }, {
    "type": "Invert",
    "invert": true
  }, {
    "type": "Convolute",
    "opaque": false,
    "matrix": [1, 1, 1, 1, 0.7, -1, -1, -1, -1]
  }, {
    "type": "Convolute",
    "opaque": false,
    "matrix": [0, -1, 0, -1, 5, -1, 0, -1, 0]
  }, {
    "type": "Brownie"
  }, {
    "type": "Brightness",
    "brightness": 0.35
  }
]
例如,我需要找到type属性值为Invert的项的索引号。所以在这种情况下,输出应该是2。我只需要搜索类型键的值。

您可以使用findIndex方法,方法是将提供的回调函数作为参数传递

设arr=[{type:Grayscale,mode:average},{type:Sepia},{type:Invert,Invert:true},{type:volume,不透明:false,矩阵:[1,1,1,1,0.7,-1,-1,-1,-1,-1]},{type:volume,不透明:false,矩阵:[0,-1,0,-1,0]},{type:Brownie},{type:Brightness,Brightness:0.35},key='type';
console.logarr.findIndexelem=>elem[key]=='Invert' 下面是代码的一个简短片段

var sample=[{type:Grayscale,mode:average},{type:Sepia},{type:Invert,Invert:true},{type:Convolume,不透明:false,矩阵:[1,1,1,0.7,-1,-1,-1,-1,-1]},{type:Convolume,不透明:false,矩阵:[0,-1,0,-1,0]},{type:Brownie},{type:Brightness,Brightness:0.35}] 函数findIndexdata,键域,值{ 返回数据.indexOfdata.findfunctionel,索引{ 返回el[keyfield]==值; }; }
console.logfindIndexample,“type”,“Invert” 您可以使用下划线或loadash_uuu包。它支持阵列操作的多种功能

const _ = require('lodash')

let your_array=  [
      {"type":"Grayscale","mode":"average"},
      {"type":"Sepia"},
      {"type":"Invert","invert":true},
      {"type":"Convolute","opaque":false,"matrix":[1,1,1,1,0.7,-1,-1,-1,-1]},
      {"type":"Convolute","opaque":false,"matrix":[0,-1,0,-1,5,-1,0,-1,0]},
      {"type":"Brownie"},
      {"type":"Brightness","brightness":0.35}
    ];
    let objectIndex = _.findIndex(your_array, each_element=> each_element.type == "Invert");
    alert(objectIndex)

我只有类型键值才能找到索引。您是否只需要找到类型为Invert的第一个实例,或者数组是否可以包含许多这样的对象?因此,您希望遍历数组中的每个对象,直到找到一个类型属性为Invert的对象,然后,您希望在该项出现的数组中输出索引。如果需要查找多个事件,请继续循环。听起来不太难,你自己试过什么吗?我的意思是找到我有keytype:value的项的索引号,在上面的示例中,value是inverte,如果OP想要'Type'而不是'inverte',该怎么办?只需要注意,这在任何版本的Internet Explorer中都不受支持-请参阅,如果你需要支持IE,您将需要链接中显示的多边形填充为什么要删除对象分解???这是答案中最好的部分!既然findIndex很有名…@ADyson,你认为我们什么时候可以展望未来,干脆把这个浏览器埋了吧?。任何需要为IE编写代码的人都应该知道,polyfills和transpiling是他们需要担心的事情。@GerardoFurtado,因为我认为OP希望以友好的方式提供属性。lodash的链接