In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "what is the method of adding elements to the end of an array in JavaScript". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "what is the method of adding elements to the end of an array in JavaScript?"
Add methods: 1, the use of push () method, syntax "array.push (element 1, element 2..., element X)"; 2, the use of splice () method, syntax "array.splice (arr.length,0, element 1, element X)".
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
The method of adding elements to the end of an array in JavaScript
1. The push () method adds one or more elements to the end of the array and returns a new length.
1), grammar:
The arrayObject.push (newelement1,newelement2,....,newelementX) parameter is required to describe newelement1. The first element to add to the array. Newelement2 is optional. The second element to add to the array. NewelementX is optional. Multiple elements can be added.
2) return value:
The new length after adding the specified value to the array.
3) description:
The push () method adds its parameters sequentially to the tail of the arrayObject. It directly modifies the arrayObject instead of creating a new array.
4), example:
Var arr = new Array (3) arr [0] = "ZhangQian" arr [1] = "LinFang" arr [2] = "HaiKun" console.log (arr); / / ["ZhangQian", "LinFang", "HaiKun"] console.log (arr.push ("C")); / / 4console.log (arr); / / [ZhangQian "," LinFang "," HaiKun "," C "] console.log (arr.push (" A "," B ")); / / 6console.log (arr) / ["ZhangQian", "LinFang", "HaiKun", "A", "B", "C"]
2. The splice () method adds / removes items to / from the array, and then returns the deleted items.
1), grammar:
The arrayObject.splice (index,howmany,item1,.,itemX) parameter is required to describe index. An integer that specifies the location of the added / deleted items, using negative numbers to specify the location from the end of the array. Howmany is required. The number of items to delete. If set to 0, the item is not deleted. Item1,..., itemX is optional. A new item added to the array.
2) return value:
The type description Array contains a new array of deleted items, if any.
3) description:
The splice () method deletes zero or more elements starting at index and replaces those deleted elements with one or more values declared in the parameter list.
If you delete an element from arrayObject, an array containing the deleted element is returned.
The splice () method modifies the array directly.
4), example:
Var arr = ["A", "ZhangQian", "LinFang", "HaiKun"]; arr.splice (1Magne0, "B", "C"); console.log (arr); arr.splice (arr.length,0, "D", "C"); console.log (arr)
To add an element to the end of an array using the splice () method, the index value needs to be set to the array length value.
At this point, I believe you have a deeper understanding of "what is the method of adding elements to the end of an array in JavaScript". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.