Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 12, 2023 01:49
Show Gist options
  • Save code-boxx/1a0a8c8ba57fa6faf730aedc40ba7f3d to your computer and use it in GitHub Desktop.
Save code-boxx/1a0a8c8ba57fa6faf730aedc40ba7f3d to your computer and use it in GitHub Desktop.
Javascript Add Elements To Array

JAVASCRIPT ADD ELEMENTS TO ARRAY

https://code-boxx.com/add-elements-javascript-array/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Push Array</title>
<script>
// (A) PUSH - ADD TO END
var arr = ["First", "Second"];
arr.push("Third");
console.table(arr); // First, Second, Third
// (B) WE CAN ALSO ADD MULTIPLE AT ONCE
arr.push("Forth", "Fifth");
console.table(arr); // First, Second, Third, Forth, Fifth
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Unshift Array</title>
<script>
// (A) UNSHIFT - ADD TO FRONT
var arr = ["First", "Second"];
arr.unshift("Third");
console.table(arr); // Third, First, Second
// (B) WE CAN ALSO ADD MULTIPLE AT ONCE
arr.unshift("Forth", "Fifth");
console.table(arr); // Forth, Fifth, Third, First, Second
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Append Statement</title>
<script>
var arr = ["First", "Second"];
arr[arr.length] = "Third";
console.table(arr); // First, Second, Third
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Concat Array</title>
<script>
// (A) TWO DIFFERENT ARRAYS
var arrA = ["First", "Second"];
var arrB = ["Third", "Forth"];
// (B) CONCAT COMBINE
var combined = arrA.concat(arrB);
console.table(combined); // First, Second, Third, Forth
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Splice</title>
<script>
// (A) ONE PARAMETER
var arr = ["First", "Second", "Third", "Forth", "Fifth"];
arr.splice(2); // keep first 2 elements
console.table(arr); // First, Second
// (B) TWO PARAMETERS
arr = ["First", "Second", "Third", "Forth", "Fifth"];
arr.splice(1, 2); // remove 2 elements from index 1
console.table(arr); // First, Forth, Fifth
// (C) THREE OR MORE PARAMETERS
// (C1) INSERT SINGLE
arr = ["First", "Second", "Third"];
arr.splice(1, 0, "FOO"); // insert "foo" at index 1
console.table(arr); // First, FOO, Second, Third
// (C2) INSERT MULTIPLE
arr = ["First", "Second", "Third"];
arr.splice(1, 0, "FOO", "BAR"); // insert "foo", "bar" at index 1
console.table(arr); // First, FOO, BAR, Second, Third
// (C3) REPLACE
arr = ["First", "Second", "Third"];
arr.splice(0, 2, "FOO"); // remove 2 elements from index 0, then insert "foo"
console.table(arr); // FOO, Third
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Join Array</title>
<script>
// (A) AN ARRAY
var elements = ["Wind", "Water", "Fire", "Earth"];
// (B) JOIN ARRAY INTO STRING
var quote = elements.join(" ");
console.log(quote); // Wind Water Fire Earth
// WE CAN ALSO SPECIFY THE "JOINTS"
// quote = elements.join("-");
// console.log(quote); // Wind-Water-Fire-Earth
// (C) COMPLETE THE STORY
quote += ". Long ago, the four nations lived together in harmony.";
console.log(quote); // Wind Water Fire Earth. Long ago...
</script>
</head>
<body></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment