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]

0

4 Answers 4

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'];
Sign up to request clarification or add additional context in comments.

1 Comment

Just a pointer, if you wish to update same array, splice is recommended.
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);

10 Comments

What are you answering? Same you could have written in comments.
@Peterson I dont think he has permission.
@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)
@Peterson Lets take it easy on him. He is still new on SO. We should help him evolve.
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.
|
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)

4 Comments

This will remove only one element from the array
Nope. This will remove all elements after that index. You can run snippet to test
@KiJéy Did you try the snippet? Also I have already shared reference link. Do visit it.
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
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);

1 Comment

@Rajesh Shall i know where am I missing var ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.