Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change the array [1,2,3,4] to [1,3,4]

Tags:

javascript

any simply way ?

this is my code:

var a=[1,2,3,4]
        a.slice(0,1)
        alert( a)

and it print [1,2,3,4]

thanks

like image 711
zjm1126 Avatar asked Apr 05 '10 08:04

zjm1126


People also ask

How do you change an element in an array?

To replace an element in an array:Use the indexOf() method to get the index of the element you want to replace. Call the Array. splice() method to replace the element at the specific index. The array element will get replaced in place.

How do you reshape the size of an array?

reshape() reshape() function is used to create a new array of the same size (as the original array) but of different desired dimensions. reshape() function will create an array with the same number of elements as the original array, i.e. of the same size as that of the original array.

How do you rearrange an array in C++?

Approach used in the below program is as followsDeclare a variable as max_val and set it with arr[size - 1] + 1. Start loop FOR from i to 0 till i less than size. Inside the loop, check IF i % 2 = 0 then set arr[i] to arr[i] + (arr[max] % max_val) * max_val and decrement the max by 1.


1 Answers

You're looking for the splice() method:

var a=[1,2,3,4];
a.splice(1,1);
alert(a);        // -> 1,3,4
like image 197
Andy E Avatar answered Sep 28 '22 19:09

Andy E