0

I have an array of object as follow :

[ {label: "<p>Opacity | %<br/></p>", customRow: false, id: 0, items: Array(13)},
 {label: "Brand_hs_id", customRow: false, id: 0, items: Array(13)},
 {label: "<p>PPI |</p>", customRow: false, id: 0, items: Array(13)},
{label: "Brightness | %", customRow: false, id: 0, items: Array(13)
]

I want to findIndex of object where label value matches. for example if I pass the "Brightness | %" it should return 3.

I checked

const array1 = [5, 12, 8, 130, 44];

const isLargeNumber = (element) => element > 13;

console.log(array1.findIndex(isLargeNumber));
// expected output: 3

but not sure how to do it in array of objects .

rahularyansharma
  • 11,156
  • 18
  • 79
  • 135
  • The argument of the callback will be an object. Just access the property and compare it -> [Working with objects - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_objects) – Andreas Jun 24 '21 at 08:56
  • 1
    [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – Andreas Jun 24 '21 at 09:00

2 Answers2

0

Try something like as follows:

let val = [{id:"1002", address:"usa"},
           {id:"1004", address:"canada"},
           {id:"1006", address:"sweden"}];
    
let index = val.findIndex(x => x.address === "canada");

console.log(index); //Here index will be returned
AT-2017
  • 3,114
  • 3
  • 23
  • 39
  • Always use `var`, `let`, or `const` to avoid creating leaked global. Use `strict` mode to see these errors. – onlit Jun 24 '21 at 09:02
  • `const` should be used here, as you'll not be reassigning the values. – onlit Jun 24 '21 at 09:03
-2

do it like this

const array1 = [{age:5}, {age:12},{age:8} , {age:130}, {age:44}];
const isLargeNumber = (element) => element.age > 13;

console.log(array1.findIndex(isLargeNumber));
Prakash Harvani
  • 1,007
  • 7
  • 18