Which of the following method call gives the position of X that occurs after?

That’s natural, because

let arr = ["I", "study", "JavaScript"];

// from index 2
// delete 0
// then insert "complex" and "language"
arr.splice[2, 0, "complex", "language"];

alert[ arr ]; // "I", "study", "complex", "language", "JavaScript"
3 removes a value by the
let arr = ["I", "study", "JavaScript"];

// from index 2
// delete 0
// then insert "complex" and "language"
arr.splice[2, 0, "complex", "language"];

alert[ arr ]; // "I", "study", "complex", "language", "JavaScript"
4. It’s all it does. Fine for objects. But for arrays we usually want the rest of elements to shift and occupy the freed place. We expect to have a shorter array now.

So, special methods should be used.

The arr.splice method is a swiss army knife for arrays. It can do everything: insert, remove and replace elements.

The syntax is:

arr.splice[start[, deleteCount, elem1, ..., elemN]]

It modifies

let arr = ["I", "study", "JavaScript"];

// from index 2
// delete 0
// then insert "complex" and "language"
arr.splice[2, 0, "complex", "language"];

alert[ arr ]; // "I", "study", "complex", "language", "JavaScript"
5 starting from the index
let arr = ["I", "study", "JavaScript"];

// from index 2
// delete 0
// then insert "complex" and "language"
arr.splice[2, 0, "complex", "language"];

alert[ arr ]; // "I", "study", "complex", "language", "JavaScript"
6: removes
let arr = ["I", "study", "JavaScript"];

// from index 2
// delete 0
// then insert "complex" and "language"
arr.splice[2, 0, "complex", "language"];

alert[ arr ]; // "I", "study", "complex", "language", "JavaScript"
7 elements and then inserts
let arr = ["I", "study", "JavaScript"];

// from index 2
// delete 0
// then insert "complex" and "language"
arr.splice[2, 0, "complex", "language"];

alert[ arr ]; // "I", "study", "complex", "language", "JavaScript"
8 at their place. Returns the array of removed elements.

This method is easy to grasp by examples.

Let’s start with the deletion:

let arr = ["I", "study", "JavaScript"];

arr.splice[1, 1]; // from index 1 remove 1 element

alert[ arr ]; // ["I", "JavaScript"]

Easy, right? Starting from the index

let arr = ["I", "study", "JavaScript"];

// from index 2
// delete 0
// then insert "complex" and "language"
arr.splice[2, 0, "complex", "language"];

alert[ arr ]; // "I", "study", "complex", "language", "JavaScript"
9 it removed
let arr = ["I", "study", "JavaScript"];

// from index 2
// delete 0
// then insert "complex" and "language"
arr.splice[2, 0, "complex", "language"];

alert[ arr ]; // "I", "study", "complex", "language", "JavaScript"
9 element.

In the next example we remove 3 elements and replace them with the other two:

let arr = ["I", "study", "JavaScript", "right", "now"];

// remove 3 first elements and replace them with another
arr.splice[0, 3, "Let's", "dance"];

alert[ arr ] // now ["Let's", "dance", "right", "now"]

Here we can see that

let arr = [1, 2, 5];

// from index -1 [one step from the end]
// delete 0 elements,
// then insert 3 and 4
arr.splice[-1, 0, 3, 4];

alert[ arr ]; // 1,2,3,4,5
1 returns the array of removed elements:

let arr = ["I", "study", "JavaScript", "right", "now"];

// remove 2 first elements
let removed = arr.splice[0, 2];

alert[ removed ]; // "I", "study" 

Chủ Đề