Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the skills of copying JS arrays

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "JS array copy skills", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what are the JS array copy skills" this article.

Tip 1-use the Array.slice method

Const numbers = [1,2,3,4,5]

Const copy = numbers.slice ()

Copy.push (6) / / add a new entry to prove that the original array will not be modified

Console.log (copy)

Console.log (numbers)

/ / output

/ / [1, 2, 3, 4, 5, 6]

/ / [1, 2, 3, 4, 5]

Tip 2-use the Array.map method

Const numbers = [1,2,3,4,5]

Const copy = numbers.map (num = > num)

Copy.push (6) / / add a new entry to prove that the original array will not be modified

Console.log (copy)

Console.log (numbers)

/ / output

/ / [1, 2, 3, 4, 5, 6]

/ / [1, 2, 3, 4, 5]

Tip 3-use the Array.from method

Const numbers = [1,2,3,4,5]

Const copy = Array.from (new Set (numbers))

Copy.push (6); / / add a new item to prove that the original array will not be modified

Console.log (copy)

Console.log (numbers)

/ / output

/ / [1, 2, 3, 4, 5, 6]

/ / [1, 2, 3, 4, 5]

Tip 4-use the unfold operator

Const numbers = [1,2,3,4,5]

Const copy = [... numbers]

Copy.push (6); / / add a new item to prove that the original array will not be modified

Console.log (copy)

Console.log (numbers)

/ / output

/ / [1, 2, 3, 4, 5, 6]

/ / [1, 2, 3, 4, 5]

Tip 5-use Array.of methods and unfold operators

Const numbers = [1,2,3,4,5]

Const copy = Array.of (. Numbers)

Copy.push (6); / / add a new item to prove that the original array will not be modified

Console.log (copy)

Console.log (numbers)

/ / output

/ / [1, 2, 3, 4, 5, 6]

/ / [1, 2, 3, 4, 5]

The Array.of () method creates a new array instance with a variable number of arguments, regardless of the number or type of parameters. The difference between the Array.of () and Array constructors is that they deal with integer parameters: Array.of (7) creates an array with a single element of 7, while Array (7) creates an empty array of length 7 (note: this is an array of seven empty, not an array of seven undefined).

Array.of (7); / / [7]

Array.of (1,2,3); / / [1,2,3]

Array (7); / / [,]

Array (1,2,3); / / [1,2,3]

Tip 6-use Array constructors and unfold operators

Const numbers = [1,2,3,4,5]

Const copy = new Array (. Numbers)

Copy.push (6); / / add a new item to prove that the original array will not be modified

Console.log (copy)

Console.log (numbers)

/ / output

/ / [1, 2, 3, 4, 5, 6]

/ / [1, 2, 3, 4, 5]

Tip 7-use deconstruction

Const numbers = [1,2,3,4,5]

Const [... copy] = numbers

Copy.push (6); / / add a new item to prove that the original array will not be modified

Console.log (copy)

Console.log (numbers)

/ / output

/ / [1, 2, 3, 4, 5, 6]

/ / [1, 2, 3, 4, 5]

Tip 8-use the Array.concat method

Const numbers = [1,2,3,4,5]

Const copy = numbers.concat ()

Copy.push (6); / / add a new item to prove that the original array will not be modified

Console.log (copy)

Console.log (numbers)

/ / output

/ / [1, 2, 3, 4, 5, 6]

/ / [1, 2, 3, 4, 5]

Tip 9-use Array.push methods and unfold operators

Const numbers = [1,2,3,4,5]

Let copy = []

Copy.push (. Numbers)

Copy.push (6); / / add a new item to prove that the original array will not be modified

Console.log (copy)

Console.log (numbers)

/ / output

/ / [1, 2, 3, 4, 5, 6]

/ / [1, 2, 3, 4, 5]

Tip 10-use Array.unshift methods and unfold operators

Const numbers = [1,2,3,4,5]

Let copy = []

Copy.unshift (. Numbers)

Copy.push (6); / / add a new item to prove that the original array will not be modified

Console.log (copy)

Console.log (numbers)

/ / output

/ / [1, 2, 3, 4, 5, 6]

/ / [1, 2, 3, 4, 5]

Tip 11-use Array.forEach methods and unfold operators

Const numbers = [1,2,3,4,5]

Let copy = []

Numbers.forEach ((value) = > copy.push (value))

Copy.push (6); / / add a new item to prove that the original array will not be modified

Console.log (copy)

Console.log (numbers)

/ / output

/ / [1, 2, 3, 4, 5, 6]

/ / [1, 2, 3, 4, 5]

Tip 12-use for loops

Const numbers = [1,2,3,4,5]

Let copy = []

For (let I = 0; I

< numbers.length; i++) {   copy.push(numbers[i]);   }   copy.push(6); // 添加新项以证明不会修改原始数组   console.log(copy);   console.log(numbers);   // 输出   // [1, 2, 3, 4, 5, 6]   // [1, 2, 3, 4, 5]   技巧 13 - 使用 Array.reduce 方法   这个做法是可行,但比较多余,少用   const numbers = [1, 2, 3, 4, 5];   const copy = numbers.reduce((acc, x) =>

{acc.push (x); return acc;}, [])

Copy.push (6); / / add a new item to prove that the original array will not be modified

Console.log (copy)

Console.log (numbers)

/ / output

/ / [1, 2, 3, 4, 5, 6]

/ / [1, 2, 3, 4, 5]

Tip 14-use the old apply method

Const numbers = [1,2,3,4,5]

Let copy = []

Array.prototype.push.apply (copy, numbers)

Copy.push (6); / / add a new item to prove that the original array will not be modified

Console.log (copy)

Console.log (numbers)

/ / output

/ / [1, 2, 3, 4, 5, 6]

/ / [1, 2, 3, 4, 5]

These are all the contents of the article "what are the skills for copying JS arrays?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report