😁Array find, Tìm phần tử cuối cùng thỏa mãn điều kiện (ok)

https://stackoverflow.com/questions/40929260/find-last-index-of-element-inside-array-by-certain-condition

var d = [{'a': "something", 'b':12}, {'a': "something", 'b':12}, {'a': "somethingElse", 'b':12}, {'a': "something", 'b':12}, {'a': "somethingElse", 'b':12}]

function findLastIndex(array, searchKey, searchValue) {
  var index = array.slice().reverse().findIndex(x => x[searchKey] === searchValue);
  var count = array.length - 1
  var finalIndex = index >= 0 ? count - index : index;
  console.log(finalIndex)
  return finalIndex;
}

findLastIndex(d, 'a', 'something')
findLastIndex(d, 'a', 'nothing')

Last updated

Navigation

Lionel

@Copyright 2023