Home Web Front-end JS Tutorial JavaScript method to remove array elements and reduce length_javascript tips

JavaScript method to remove array elements and reduce length_javascript tips

May 16, 2016 pm 05:23 PM
array elements

Copy code The code is as follows:

//Array removal length method

var array =[];
array[0]="Zhang San";
array[1]="Li Si";
array[2]="Wang Wu";
array[3] ="Zhao Liu";
array[4]="Baidu";

function remove(array,index){
if(index<=(array.length-1)){
for(var i=index;iarray[i]=array[i 1];
}
}else{
throw new Error(' Array index out of bounds!for function is:remove');
}
array.length=array.length-1;
return array;
}


try{
array=remove(array,2);
array=remove(array,2);
alert(array.length);
alert(array);
}catch( e){
alert(e);
}
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to find the maximum and minimum value of array elements in Java How to find the maximum and minimum value of array elements in Java Oct 08, 2023 am 09:44 AM

Use the `Arrays.stream()` function in Java to convert an array into a stream, and then use the `min()` and `max()` functions to calculate the minimum and maximum values.

Which array element has the smallest sum of absolute differences? Which array element has the smallest sum of absolute differences? Aug 29, 2023 am 10:09 AM

Here we will see an interesting problem. We have an array 'a' containing N elements. We need to find an element x that minimizes the value of |a[0]-x|+|a[1]-x|+...+|a[n-1]-x|. Then we need to find the minimized sum. Suppose the array is: {1,3,9,6,3}, and now x is 3. So the sum is |1-3|+|3-3|+|9-3|+|6-3|+|3-3|=11. To solve this problem, we need to choose the median of the array as x. If the size of the array is even, there will be two median values. They are all the best choices for x. Algorithm minSum(arr,n)begin &

How to use implode function to concatenate array elements into string in PHP How to use implode function to concatenate array elements into string in PHP Jun 26, 2023 pm 02:02 PM

In PHP programming, the implode function is a very commonly used function that can concatenate elements in an array into a string. Using this function can save developers from writing a lot of code to connect strings, making it more efficient. The basic syntax of implode is: stringimplode(string$glue,array$pieces). This function receives two parameters: $glue represents the separator to connect the array elements, and $pieces represents

Concatenate array elements into a delimited string using PHP's implode() function Concatenate array elements into a delimited string using PHP's implode() function Nov 04, 2023 pm 02:37 PM

Use PHP's implode() function to connect array elements into a delimited string. The code example is as follows: &lt;?php//Define an array $array=array('apple','banana','orange'); //Use the implode() function to connect the array elements into a delimited string $delimiter=',';//Define the delimiter $result=im

In C language, what are the array elements that appear multiple times? In C language, what are the array elements that appear multiple times? Sep 05, 2023 am 09:05 AM

Array is a container that contains elements of the same data type, and the length needs to be defined in advance. Elements in an array can appear in any order and any number of times. So, in this program, we will find the elements that appear multiple times in an array. Problem description - We have been given an array arr[], we need to find the recurring elements in the array and print them. Let us take an example to understand better. Example: Input:arr[]={5,11,11,2,1,4,2}Output:112 Explanation We have an array arr containing some elements, first we will compare the next element in the repeat function. Repeat function is used to find duplicate elements in an array. In the repeat function we use

Python program to recursively linearly search elements in an array Python program to recursively linearly search elements in an array Aug 20, 2023 pm 11:22 PM

Linear search is the simplest way to search for elements in an array. It is a sequential search algorithm that starts from one end and checks each element of the array until the required element is found. Recursion is when a function calls itself, when using recursive functions we need to use any loop to generate iterations. The syntax below demonstrates how a simple recursive function works. defrerecursiveFun():Statements...rerecursiveFun()...rerecursiveFun recursively linearly searches for an element. Recursively linearly searches for an element from an array. This can only be achieved by using functions. In Python, to define a function, we need to use the def keyword. exist

Get the element keys in an array using the PHP array_keys() function Get the element keys in an array using the PHP array_keys() function Jun 27, 2023 am 08:54 AM

In the process of using PHP to develop, it is often necessary to operate arrays. In an array, we usually need to get the key value of the element to facilitate subsequent operations. For this purpose, PHP provides a very convenient function array_keys(), which can quickly obtain the keys of elements from an array. The usage of the array_keys() function is very simple. Its basic syntax is as follows: arrayarray_keys(array$array[,mixed$search_valu

Checks whether the number formed by concatenating array elements is a hashed number Checks whether the number formed by concatenating array elements is a hashed number Aug 25, 2023 pm 08:17 PM

In this question, we are given an array of integers. We need to combine all the elements into an integer and check if it is a Harshad number. Before we move on to the solution, let us understand the Harshad number. All numbers are Harshad numbers, which are divisible by the sum of their numbers. For example, 12 is Harshad's number because 12 is divisible by 3, which is the sum of 1+2. To solve this problem, we can add all the array elements and then check if the result is a Harshad number. Problem Statement - We are given an array of integers. We need to combine all the elements into a number and check if the combined number is a Harshad number. Example input – arr={1,35,69,60}; output – yes

See all articles