JavaScript array manipulation

In Java, it is difficult to do easily with List.add, List.remove, and List.addAll with JavaScript, so I tried to summarize frequently used array operations.

Add an element to the end of the array

Add element ʻitem to array ʻarray. In Java, List.add (). This operation modifies ʻarray` itself.

array.push(item)

Remove an element from the array

Delete the element ʻitem in the array ʻarray. In Java, List.remove (item). This operation modifies ʻarray` itself.

const index = array.indexOf(item)
if (index >= 0) {
  array.splice(index, 1)
}

Add an element to the beginning of the array

Add element ʻitem to ** beginning ** of array ʻarray. In Java, List.add (0, item). This operation modifies ʻarray` itself.

array.unshift(item)

Join the array after the array

Add the array b to the array ʻa. In Java, List.addAll (b). This operation modifies ʻa itself.

a.splice(a.length, 0, ...b)

Create an array c by adding the array b to the array ʻa. ʻA itself ** does not change **.

c = a.concat(b)

Recommended Posts

JavaScript array manipulation
Ruby array manipulation
Array
Array
Hanachan in Ruby (non-destructive array manipulation)
[Java] array
[Ruby] Array
Java array
Array practice
Java array
java (array)
javascript AOP
Java array
[Java] Array
Array practice 2
Java array
JavaScript basics
12 of Array
JavaScript function
java array
[Java] Array