Home  >  Article  >  Web Front-end  >  How to return an array of output javascript

How to return an array of output javascript

WBOY
WBOYOriginal
2023-05-12 10:03:36763browse

In JavaScript, array is a commonly used data type. We can use arrays to store a series of data. When writing JavaScript programs, you often need to process arrays, such as traversing arrays, modifying arrays, sorting arrays, etc. When processing arrays, sometimes it is necessary to store the processing results of a certain function into a new array, or to modify the original array. The modified array is then returned to the caller. This article explains how to return an output array in JavaScript.

  1. Use the return statement to return the array

In JavaScript, a function returns the execution result of the function through the return statement. A function can return any type of value, including strings, numbers, objects, Boolean values, and arrays. If you want to return an array in a function, you can use the array as the return value of the return statement. For example, the following code defines a function that accepts an array as a parameter and returns a subarray of that array with indices from 0 to 2.

function getSubArray(arr) {
  return arr.slice(0, 3);
}

let arr = [1, 2, 3, 4, 5];
let subArr = getSubArray(arr); // [1, 2, 3]

In the above example, the getSubArray function returns the subarray [1, 2, 3] of the array arr through the return statement. We store the subarray returned by getSubArray in the variable subArr. The return array is returned through the return statement.

  1. Using array objects to call functions

In JavaScript, an array is an object that has some built-in methods that can be used to manipulate arrays. These methods return a modified array, and we can use these methods to return the output array. The following are some commonly used array methods:

  • push(): Adds one or more elements to the end of the array.
  • pop(): Delete the last element of the array.
  • shift(): Delete the first element of the array.
  • unshift(): Add one or more elements to the beginning of the array.
  • splice(): Add or remove elements from the array.
  • slice(): Returns a subarray of the array.

For example, the following code defines a function that accepts an array as a parameter and adds a new element 1 to the end of the array. The function then returns the modified array to the caller.

function appendOne(arr) {
  arr.push(1);
  return arr;
}

let arr = [1, 2, 3];
let newArr = appendOne(arr); // [1, 2, 3, 1]

In the above example, we defined a function appendOne, which adds a new element 1 to the end of the array by calling the push method of the array object. The function then uses a return statement to return the modified array to the caller. We store the returned array in the variable newArr.

  1. Using the spread operator of ES6

In ES6, we can use the spread operator (...) to expand an array. When we pass an array as a parameter to a function, we can use the spread operator to expand the array into a parameter list. Furthermore, we can also use the spread operator to combine two or more arrays into one array.

The following is an example that shows how to pass an array as a parameter to a function using the spread operator:

function sum(...numbers) {
  let result = 0;
  for (let num of numbers) {
    result += num;
  }
  return result;
}

let arr = [1, 2, 3, 4];
let total = sum(...arr); // 10

In the above example, we have defined a function sum that uses The for loop iterates through the array of parameters passed to it and calculates their sum. We use the spread operator to expand the array arr into a parameter list. We then store the function’s return value of 10 in the total variable.

If we want to merge two arrays into one array, we can use the spread operator. Here is an example:

let arr1 = [1, 2];
let arr2 = [3, 4];
let arr3 = [...arr1, ...arr2]; // [1, 2, 3, 4]

In the above example, we expand two arrays arr1 and arr2 into a parameter list and merge them into a new array arr3. The new array arr3 contains all elements of the original arrays arr1 and arr2.

Summary

In JavaScript, we can use return statements, built-in methods of array objects, spread operators and other methods to return the modified array to the caller. Either way, it helps us better handle, modify, and return arrays when writing JavaScript programs.

The above is the detailed content of How to return an array of output javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:jquery search method callNext article:jquery search method call