JavaScript Array findLast()

ການອະທິບາຍແລະນຳໃຊ້

findLast() ວິທີການຈະກັບຄືນຕົວອັນສຸດທ້າຍທີ່ຜ່ານການທົດລອງ

findLast() ວິທີການຈະດຳເນີນວິທີການຕໍ່ສະໂຫຍດທຸກຢ່າງ

ຖ້າບໍ່ພົບສະໂຫຍດໃດໆfindLast() ວິທີການຈະກັບຄືນ undefined

findLast() ວິທີການຈະບໍ່ດຳເນີນວິທີການຕໍ່ສະໂຫຍດທີ່ບໍ່ມີຢູ່

findLast() ວິທີການຈະບໍ່ປ່ຽນແປງສະໂຫຍດຕົ້ນເຫຼົ່ານັ້ນ

ວິທີການຄົ້ນຫາສະໂຫຍດ

ວິທີການ ຄົ້ນຫາສິ່ງ
indexOf() ບ່ອນທີ່ທໍາອິດທີ່ມີຄູ່ມືກັບຄຳສະເພາະ
lastIndexOf() ບ່ອນທີ່ສຸດທ້າຍທີ່ມີຄູ່ມືກັບຄຳສະເພາະ
find() ຕົວອັນສຸດທ້າຍທີ່ຜ່ານການທົດລອງ
findIndex() ບ່ອນທີ່ທໍາອິດທີ່ຜ່ານການທົດລອງ
findLast() The value of the last tested element.
findLastIndex() ບ່ອນທີ່ສຸດທ້າຍທີ່ຜ່ານການທົດລອງ

ຄວາມຄົງສິ່ງ

ຕົວຢ່າງ 1

ຄົ້ນຫາຕົວອັນສຸດທ້າຍທີ່ຫຼາຍກວ່າ 18

const ages = [3, 10, 18, 20];
function checkAge(age) {
  return age > 18;
}
function myFunction() {
  document.getElementById("demo").innerHTML = ages.findLast(checkAge);
}

Try it yourself

ຕົວຢ່າງ 2

ຄົ້ນຫາຕົວອັນສຸດທ້າຍທີ່ຫຼາຍກວ່າຈຳນວນສະເພາະ

<p><input type="number" id="ageToCheck" value="18"></p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
const ages = [4, 12, 16, 20];
function checkAge(age) {
  return age > document.getElementById("ageToCheck").value;
}
function myFunction() {
  document.getElementById("demo").innerHTML = ages.findLast(checkAge);
}
</script>

Try it yourself

Syntax

array.findLast(function(currentValue, index, arr), thisValue)

Parameters

Parameters Description
function() Required. The function run for each array element.
currentValue Required. The value of the current element.
index Optional. The index of the current element.
arr Optional. The array that the current element belongs to.
thisValue

Optional. Default is undefined.

The value of this passed to the function.

Return Value

Type Description

The value of the last tested element.

If not found, returns undefined.

Browser Support

findLast() Is a feature of ES2023.

Starting from July 2023, all modern browsers support this method:

Chrome Edge Firefox Safari Opera
Chrome 110 Edge 110 Firefox 115 Safari 16.4 Opera 96
February 2023 February 2023 July 2023 March 2023 May 2023