0

I have an object in which I need to find a particular item index number. Below is my object:

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

For example, I need to find the index number of the item which has the value Invert for the type property. So in this case, the output should be 2. I only need to search the values of the type key.

Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
Sunil Meena
  • 481
  • 3
  • 10
  • 19
  • "I have only type key value to find index" elaborate please – Dellirium Jun 26 '18 at 11:20
  • Do you only need to find the first instance where type is Invert, or could the array hold many of these objects – Dan Zuzevich Jun 26 '18 at 11:21
  • so you want to loop through each object in the array until you find one whose "type" property equals "Invert", and then you want to output the index in the array at which that item occurs. If you need to be able to find more than one occurrence, continue your loop. Doesn't sound too hard, have you tried anything yourself yet? – ADyson Jun 26 '18 at 11:21
  • i mean to find index number of item i have key("type":) value and in above example value is **Invert** – Sunil Meena Jun 26 '18 at 11:22

3 Answers3

12

You can use findIndex method, by passing a provided callback function as argument.

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'));
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
  • What about if the OP want `'Type'` instead of `'Invert'` ? – Ramesh Rajendran Jun 26 '18 at 11:24
  • 1
    only worth noting that this is not supported in any version of Internet Explorer - see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex, so if you need to support IE, you'll need the polyfill shown in the link – ADyson Jun 26 '18 at 11:24
  • Why did you remove the object destructuring??? It was the best part of the answer! (since `findIndex` is quite well known...) – Gerardo Furtado Jun 26 '18 at 11:33
  • 2
    @ADyson When do you think we could look forward, and just bury this browser?. Anyone who needs to code for IE, should know that polyfills & transpiling is something they need to worry about. – Keith Jun 26 '18 at 11:34
  • @GerardoFurtado, because I think the OP wants the property dinamically given. – Mihai Alexandru-Ionut Jun 26 '18 at 11:34
2

Here is a short snippet for the code.

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'));
Dellirium
  • 1,362
  • 16
  • 30
0

you can use underscore or loadash(_) package. It has multiple functionality support for array operations.

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)
Prajwal
  • 319
  • 4
  • 10