How to get the Index of an array of objects by key and value (name) in JavaScript

2 Answers

0 votes
const array = [
   {task: 984, 
    job: "Write DB Stored Procedures",
    completed: true},
   {task: 371,
    job: "Write Search Queries",
    completed: false},
   {task: 264,
    job: "Write Crawl Algorithm",
    completed: true}
  ]


function getIndex() {
  let index = array.map(function (e) {
    return e.job;
  }).indexOf('Write Search Queries');
  
  console.log(index);
}

getIndex();
 
 
 
   
     
/*
run:
     
1
     
*/

 



answered Jan 31, 2021 by avibootz
0 votes
const array = [
   {task: 984, 
    job: "Write DB Stored Procedures",
    completed: true},
   {task: 371,
    job: "Write Search Queries",
    completed: false},
   {task: 264,
    job: "Write Crawl Algorithm",
    completed: true}
  ]

let index = array.findIndex( element => {
  if (element.job === 'Write Crawl Algorithm') {
    return true;
  }
});


console.log(index);
 
 
 
   
     
/*
run:
     
2
     
*/

 



answered Jan 31, 2021 by avibootz

Related questions

...