Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 12, 2023 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/9637e89935ff157d9df4662f26c99a55 to your computer and use it in GitHub Desktop.
Save code-boxx/9637e89935ff157d9df4662f26c99a55 to your computer and use it in GitHub Desktop.
Javascript Nested Array - Create Push Pop Loop Check
## JAVASCRIPT NESTED ARRAY CREATE PUSH POP LOOP
https://code-boxx.com/javascript-nested-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>Js Nested Array</title>
<script>
// (A) NESTED ARRAY
var arr = [
"Foo Bar", // first - string
123, // second - number
true, // third - boolean
["Hello", "World"], // forth - array
{ name:"Jon", gender:"M" }, // fifth - object
text => alert(text) // sixth - function
];
// (B) THE ELEMENTS
console.log(arr[0]); // Foo Bar
console.log(arr[1]); // 123
console.log(arr[2]); // true
console.log(arr[3]); // ["Hello", "World"]
console.log(arr[4]); // { name : "Jon", gender : "M" }
console.log(arr[5]); // text => alert(text)
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Js Nested Array</title>
<script>
// (A) NESTED ARRAY
var arr = [
["Foo", "Bar"],
{ name : "Jon", gender : "M" },
text => alert(text)
];
// (B) FIRST ELEMENT - ARR[0] IS AN ARRAY
console.log(arr[0][0]); // Foo
console.log(arr[0][1]); // Bar
// (C) SECOND ELEMENT - ARR[1] IS AN OBJECT
console.log(arr[1]["name"]); // Jon
console.log(arr[1]["gender"]); // M
// (D) THIRD ELEMENT - ARR[2] IS A FUNCTION
arr[2]("TEST!"); // TEST!
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Js Nested Array</title>
<script>
// (A) NESTED ARRAY
var arr = [
["Apple", "Banana"],
["Hello", "World"]
];
// (B) POP - REMOVE LAST ELEMENT
arr.pop();
console.log(arr); // [["Apple", "Banana"]]
// (C) PUSH - ADD ELEMENT
arr.push(["Job", "Jon"]);
console.log(arr); // [["Apple", "Banana"], ["Job", "Jon"]]
// (D) POP NESTED
arr[1].pop();
console.log(arr); // [["Apple", "Banana"], ["Job"]]
// (E) PUSH NESTED
arr[1].push("Joy");
console.log(arr); // [["Apple", "Banana"], ["Job", "Joy"]]
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Js Nested Array</title>
<script>
// (A) NESTED ARRAY
var arr = [
["first", "second"],
["third", "forth"]
];
// (B) LOOP NESTED ARRAY
function looper (entry) {
for (let el of entry) {
if (Array.isArray(el)) { looper(el); }
else { console.log(el); }
}
};
looper(arr);
</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Js Nested Array</title>
<script>
// (A) NESTED ARRAY
var arr = [
["first", "second"],
["third", "forth"]
];
// (B) LOOP NESTED ARRAY
function checker (entry, find) {
let result = false;
for (let el of entry) {
if (Array.isArray(el)) {
result = checker(el, find);
} else {
if (el == find) {
result = true;
break;
}
}
}
return result;
}
var exist = checker(arr, "third");
console.log(exist); // true
</script>
</head>
<body></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment