Skip to content

Instantly share code, notes, and snippets.

@Christopher2K
Created July 15, 2020 22:27
Show Gist options
  • Save Christopher2K/0cceda49f8f85b874cc07764c5bc2fbd to your computer and use it in GitHub Desktop.
Save Christopher2K/0cceda49f8f85b874cc07764c5bc2fbd to your computer and use it in GitHub Desktop.
Arrays with Javascript code snippets
// Snippets de code utilisés dans l'article
// 1.
let myArray = [];
// 2.
let myArray = [];
myArray.push("hello");
myArray.push("world");
myArray.push("today", "is", "a", "beautiful", "day", 44);
console.log(myArray);
// output: ['hello', 'world', 'today', 'is', 'a', 'beautiful', 'day', 44]
// 3.
let myArray = ["hello", "world"];
console.log(myArray);
// output: ['hello', 'world']
// 4.
let myArray = [undefined, undefined];
console.log(myArray.length);
// output: 2
// 5.
let myArray = Array(2);
console.log(myArray.length);
// output: 2
// 6.
function myFunctionWhichIsDoingNothing(element) {
console.log(`The current element is ${element}`);
}
// 7.
let myArray = ["chris", "anael", "larry", "eunice", "walter"];
for (let count = 0; count < myArray.length; count++) {
myFunctionWhichIsDoingNothing(myArray[count]);
}
// output:
// The current element is chris
// The current element is anael
// The current element is larry
// The current element is eunice
// The current element is walter
// 8.
let myArray = ["chris", "anael", "larry", "eunice", "walter"];
for (let element of myArray) {
myFunctionWhichIsDoingNothing(element);
}
// 9.
let myArray = ["chris", "anael", "larry", "eunice", "walter"];
myArray.forEach((element, index) => {
console.log(`current index is : ${index}`);
myFunctionWhichIsDoingNothing(element);
});
// output:
// current index is : 0
// The current element is chris
// current index is : 1
// The current element is anael
// current index is : 2
// The current element is larry
// current index is : 3
// The current element is eunice
// current index is : 4
// The current element is walter
// 10.
let [lastName, firstName] = ["KATOYI", "Christopher"];
console.log(`Nom: ${lastName} - Prénom: ${firstName}`);
// ouput: KATOYI Christopher
// 11.
let myArray = ["chris", "anael", "larry", "eunice", "walter"];
let myArray2 = ["nicolas", "salif"];
let newArray = myArray.concat(myArray2);
console.log(newArray);
// output: ['chris', 'anael', 'larry', 'eunice', 'walter', 'nicolas', 'salif']
// 12.
let myArray = ["chris", "anael", "larry", "eunice", "walter"];
let myArray2 = ["nicolas", "salif"];
let newArray = [...myArray, ...myArray2];
console.log(newArray);
// output: ['chris', 'anael', 'larry', 'eunice', 'walter', 'nicolas', 'salif']
// 13.
let newArray = [...myArray, ...myArray2];
let newArray = [
"chris",
"anael",
"larry",
"eunice",
"walter",
"nicolas",
"salif",
];
// 14.
let persons = [
{
name: "Christopher",
age: 24,
city: "Paris",
},
{
name: "Tobe",
age: 21,
city: "Lyon",
},
{
name: "Lydia",
age: 29,
city: "Strasbourg",
},
{
name: "Ersin",
age: 22,
city: "Marseille",
},
];
// 15.
let christopherProfile = undefined;
for (let person of persons) {
if (person.name == "Christopher") {
christopherProfile = person;
break;
}
}
// 16.
let christopherProfile = persons.find((person) => {
return person.name == "Christopher";
});
// 17.
let myArray = ["chris", "anael", "larry", "eunice", "walter"];
let found = myArray.includes("chris");
// found == true
let notFound = myArray.includes("harris");
// notFound == false
// 18.
let myArray = ["chris", "anael", "larry", "eunice", "walter"];
let found = myArray.some((elm) => elm === "chris");
// found == true
// 19.
let myValue = { name: "chris" };
let myArray = [myValue];
let notFound = myArray.includes({ name: "chris" });
// notFound == false
let found = myArray.includes(myValue);
// found == true
// 20.
let myArray = ["chris", "christine", "christian", "noah"];
let namesThatStartWithC = [];
for (name of myArray) {
if (name.startsWith("c")) {
namesThatStartWithC.push(name);
}
}
console.log(namesThatStartWithC);
// print ['chris', 'christine', 'christian']
// 21.
let myArray = ["chris", "christine", "christian", "noah"];
let namesThatStartWithC = myArray.filter((name) => name.startsWith("c"));
console.log(namesThatStartWithC);
// print ['chris', 'christine', 'christian']
// 22.
let myArray = ["chris", "christine", "christian", "noah"];
let myUppercasedArray = myArray.map((elm) => elm.toUpperCase());
console.log(myUppercasedArray);
// print ['CHRIS', 'CHRISTINE', 'CHRISTIAN', 'NOAH']
// 23.
let myArray = ["chris", "christine", "christian", "noah"];
let mySentence = myArray.reduce((acc, item) => {
return `${acc} ${item}`;
}, "Je sors aujourd'hui avec");
console.log(mySentence);
// print Je sors aujourd'hui avec chris christine christian noah
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment