0

I know this could be quite simple for someone else but I can't think of any solution.

I have an array called breadcrumb with the following elements breadcrumb = [a, b, c, d]

I also know the index of b. How do I pop all other elements from the array after index of b in JavaScript. the final array should look like this

breadcrumb = [a, b]

charles okojie
  • 708
  • 2
  • 10
  • 23

4 Answers4

2

There's the slice method in the Array prototype :

var breadcrumb = ['a', 'b', 'c', 'd'];
// in case you have to find the index of the element
var index = breadcrumb.indexOf('b');
breadcrumb = breadcrumb.slice(0, index + 1) // now breadcrumb = ['a', 'b'];
Mouradif
  • 2,666
  • 1
  • 20
  • 37
1

Im pretty sure the accepted answer from this SO question is exactly what you are looking for:

var array = ['one', 'two', 'three', 'four'];
array.length = 2;
alert(array);
Community
  • 1
  • 1
Hoi_A
  • 472
  • 1
  • 10
  • 20
  • What are you answering? Same you could have written in comments. – Deadpool Oct 06 '16 at 13:27
  • @Peterson I dont think he has permission. – Rajesh Oct 06 '16 at 13:27
  • @Rajesh yep got no permission to comment on other answers. Also just edited my answer to include the code (which is also from the link I provided) – Hoi_A Oct 06 '16 at 13:28
  • @Peterson Lets take it easy on him. He is still new on SO. We should help him evolve. – Rajesh Oct 06 '16 at 13:32
  • Why are you downvoting him? His answer is perfectly right, and the fastest one performance-wise. Changing the length of an array is part of the spec, so it's valid. – Aurelien Ribon Oct 06 '16 at 13:38
  • @AurélienRibon when downvote came, he did not have code snippet. It was just text pointing to another post. Also, its fast but it will not remove values from that location causing wastage of resources – Rajesh Oct 06 '16 at 13:41
  • Yea it was originally only text. Although even so, it's not hard to click a link to then instantly see what I basically just copied. – Hoi_A Oct 06 '16 at 13:44
  • Wow ! didn't know that was possible too. How does the garbage collection behave on the elements that aren't pointed anymore ? – Mouradif Oct 06 '16 at 16:31
  • It does not. This kind of hacks results in memory wastage. This will become an isolated non-accessible area of memory until tab is closed – Rajesh Oct 06 '16 at 17:15
  • Are you sure ? Because I just tried this in a chrome console : `var d = ['a', 'b', 'c', 'd']; d.length = 2; d.length = 4; console.log(d[2]);` it gave `undefined` – Mouradif Oct 07 '16 at 07:36
0

You should use array.splice to remove from next element

Syntax: array.splice(index, deleteCount)

var data= ['a', 'b', 'c', 'd'];
data.splice(1+1);
console.log(data)
Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • This will remove only one element from the array – Mouradif Oct 06 '16 at 13:24
  • Nope. This will remove all elements after that index. You can run snippet to test – Rajesh Oct 06 '16 at 13:25
  • @KiJéy Did you try the snippet? Also I have already shared reference link. Do visit it. – Rajesh Oct 06 '16 at 13:36
  • Yes the documentation is confusing : `array.splice(start, deleteCount[, item1[, item2[, ...]]])` vs `arr.slice([begin[, end]])` seemed obvious to me that `slice` was for keeping part of an array while `splice` was for popping a specific piece. I was wrong though. Thanks for the clarification – Mouradif Oct 06 '16 at 16:29
0

There are various ways of achieving this.

As you said you know the index of the position from where you need to pop all the elements 1.

var position=2
var breadcrumb = [a, b, c, d];
var length=breadcrumb.length;
var loop;
for(loop=position;loop<length;loop++)
breadcrumb.pop();

2. You can use slice to do this.

    var position=2;
    var breadcrumb = ["a", 'b', 'c', 'd'];
    var length=breadcrumb.length;
    var result_arr=breadcrumb.slice(0,position);

3.You can also use splice to do this

    var position=2;
    var breadcrumb = ["a", 'b', 'c', 'd'];
    var length=breadcrumb.length;
    var result_arr=breadcrumb.splice(0,position);
Ashok kumar
  • 544
  • 1
  • 5
  • 21