<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const findIndex = (id) => {
var result = -1;
var tasks = [
{
id: 1,
name: "Name 1",
status: true
},
{
id: 2,
name: "Name 2",
status: false
},
{
id: 3,
name: "Name 3",
status: true
}
];
tasks.forEach((task,index) => {
if(task.id === id) {
result = index;
}
});
return result;
};
var index = findIndex(2);
console.log(index); // 1
</script>
</body>
</html>
findIndex = (id) => {
var result = -1;
var {tasks} = this.state;
tasks.forEach((task,index) => {
if(task.id === id) {
result = index;
}
});
return result;
};