12

I have an array MyArray and I want to remove one element at position i.

I tried:

MyArray = MyArray.splice(i, 1); 

but it's returning the element I want to remove, instead of the array without the element I want to remove.

I''m sure it's a simple fix, thanks anyway.

8 Answers 8

22

Change

MyArray = MyArray.splice(i, 1); 

into

MyArray.splice(i, 1); 
Sign up to request clarification or add additional context in comments.

Comments

11

You should read the documentation on splice

Summary

Changes the content of an array, adding new elements while removing old elements.

The original array gets changed, rather than returning a new array with the specified elements removed. MyArray.splice(i, 1); should be enough.

1 Comment

ok, thanks; hopefully the question title will be seo optimized so others will get to it without having to sort through the entire documentation:)
3

splice() is a destructive method in that it has side effects aside from returning value. While it returns the removed element, it modifies the actual array you've passed in.

alert("Before: " + MyArray);
MyArray.splice(i, 1);
alert("After: " + MyArray);

Comments

2

splice edits the array in place.

var arr = [1, 2, 3, 4];
// arr is [1, 2, 3, 4];
var el = arr.splice(0, 1);
// el is [1], arr is now [2, 3, 4].

Comments

1

You've pretty much got it! however don't assign it to a new value.. just use your existing array.

MyArray.splice(i, 1);
Something = MyArray;

Hope this helps!

1 Comment

Yes, the splice method edits the existing array and returns the removed element(s) as a separate array. Check MyArray after your splice. More info: developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…
0

I had a similar problem, especially since I only wanted to remove a specific item in the array, not all items matching that item.

I created this extension for KO observable arrays, which seems to work. I haven't tried it with arrays of objects yet, but I don't see why that wouldn't work.

When I was first learning KO, I had a very hard time dealing with Arrays, so I hope this will be useful to others:

JavaScript:

ko.observableArray.fn.removeAt = function (index) {
  this.valueWillMutate();
  this.splice(index, 1);
  this.valueHasMutated();
}

HTML:

<div data-bind="foreach: Answers">
    <input type="text" data-bind="value: $data"/>
    <button 
        data-bind="click: function(){ $parent.Answers.removeAt($index()) }"                          
        title="Remove"
    >X</button>
</div>

Comments

0

Syntax

array.splice(index , howMany[, element1[, ...[, elementN]]])

Description

Changes the content of an array, adding new elements while removing old elements.

Return

An array containing the removed elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.

from Array splice method - JavaScript | MDN

Comments

0

You can now use toSpliced(). See here.

MyArray = MyArray.toSpliced(i, 1);

The toSpliced() method of Array instances is the copying version of the splice() method. It returns a new array with some elements removed and/or replaced at a given index.

Comments

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.