Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140676

Remove last element from array if it matches a particular condition

$
0
0

What's the best way to remove the last element from an array if it matches a particular value?

For instance:

components = dedupeLast(components, '<app-builder-placeholder></app-builder-placeholder>');

function dedupeLast(a, target) {
  for(let i = 0; i < a.length; i++){
    if (i === a.length) {
      if(a[i] === target)
      a.splice(i, 1);
    }
  }
}

Viewing all articles
Browse latest Browse all 140676

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>