JSFiddle - React, Tailwind, and code Playground

by helgatheviking

JavaScript

function removeLastObjectWithID9(array) {
  for (let i = array.length - 1; i >= 0; i--) {
    if (array[i].ID === 5) {
      array.splice(i, 1);
      break; // Stop the loop after removing the last matching object
    }
  }
}

const arrayOfObjects = [
{ ID: 5, name: 'Object 0' },
  { ID: 1, name: 'Object 1' },
  { ID: 3, name: 'Object 2' },
  { ID: 5, name: 'Object 3' },
  { ID: 9, name: 'Object 4' },
  { ID: 9, name: 'Object 5' },
];

removeLastObjectWithID9(arrayOfObjects);

console.log(arrayOfObjects);