【问题标题】:Find index of object in array with specific value for specific key [duplicate]在具有特定键的特定值的数组中查找对象的索引[重复]
【发布时间】:2018-12-05 02:26:02
【问题描述】:

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

[
  {
    "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。我只需要搜索type 键的值。

【问题讨论】:

  • “我只有键入键值来查找索引”请详细说明
  • 您只需要找到类型为 Invert 的第一个实例,还是该数组可以容纳许多这样的对象
  • 所以你想遍历数组中的每个对象,直到找到一个“type”属性等于“Invert”的对象,然后你想输出数组中该项目出现的索引。如果您需要能够找到不止一个事件,请继续您的循环。听起来不太难,你自己试过了吗?
  • 我的意思是找到我有 key("type":) 值的项目的索引号,在上面的例子中值是 Invert

标签: javascript arrays javascript-objects


【解决方案1】:

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

let arr = [ {"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} ], key = 'type';
console.log(arr.findIndex(elem => elem[key] == 'Invert'));

【讨论】:

  • 如果 OP 想要 'Type' 而不是 'Invert' 怎么办?
  • 唯一值得注意的是,任何版本的 Internet Explorer 都不支持此功能 - 请参阅 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…,因此如果您需要支持 IE,则需要链接中显示的 polyfill
  • 为什么要去掉对象解构???这是答案中最好的部分! (因为findIndex 是众所周知的......)
  • @ADyson 你认为我们什么时候可以向前看,然后埋葬这个浏览器?任何需要为 IE 编写代码的人都应该知道 polyfills 和 transpiling 是他们需要担心的事情。
  • @GerardoFurtado,因为我认为 OP 希望以动态方式给出该属性。
【解决方案2】:

这是代码的简短 sn-p。

var sample = [{"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}]  

function findIndex(data, keyfield, value){
  
  return data.indexOf(data.find(function(el,index){
    return el[keyfield] === value;
  }));
}

console.log(findIndex(sample, 'type', 'Invert'));

【讨论】:

    【解决方案3】:

    您可以使用下划线或 loadash(_) 包。它具有对数组操作的多种功能支持。

    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)
    

    【讨论】:

    猜你喜欢
    • 2019-02-26
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    相关资源
    最近更新 更多