/* The concat([new],...) method concatenates (joins) two or more arrays */
colors = [๐ฃ,๐ก];
colors.concat([๐ ,๐ข]);
result:
[๐ฃ,๐ก,๐ ,๐ข]
/* The slice(start, end) method returns selected elements in an array, as a new array. */
colors = [๐ฃ,๐ก,๐ ,๐ข];
new = colors.slice(1,3);
result:
[๐ก,๐ ]
/* The splice(start, delete, [new]) method adds and/or removes array elements from the original array */
//add
colors = [๐ฃ,๐ก,๐ ,๐ข];
colors.splice(1,0,๐ต);
result:
[๐ฃ,๐ต,๐ก,๐ ,๐ข]
//remove
colors = [๐ฃ,๐ก,๐ ,๐ข];
colors.splice(1,2);
result:
[๐ฃ,๐ข]
//swap
colors = [๐ฃ,๐ก,๐ ,๐ข];
colors.splice(1,2,๐ต,๐ด);
result:
[๐ฃ,๐ต,๐ด,๐ข]
/* The join(separator) method returns an array as a string and does not change the original array. */
colors = [๐ต,๐ก,๐ ,๐ข];
string = colors.join('-');
result: (string)
'๐ต-๐ก-๐ -๐ข'
/* The pop() method removes (pops) the last element of an array changing the original array and returning what was removed. */
colors = [๐ฃ,๐ด,๐ข,๐ต];
string = colors.pop();
result: (colors)
[๐ฃ,๐ด,๐ข]
return: (string)
'๐ต'
/* The shift() method removes the first item of an array, changes the original array and returning what was removed */
colors = [๐ฃ,๐ด,๐ต,๐ก];
string = colors.shift();
result: (colors)
[๐ด,๐ต,๐ก]
return: (string)
'๐ฃ'
/* The push() method adds new items to the end of an array, changes the length of the original array and returns the new length */
colors = [๐ด,๐ต,๐ก];
colors.push(๐ข);
result: (colors)
[๐ด,๐ต,๐ก,๐ข]
return: (length)
4
/* The unshift() method adds new elements to the beginning of an array, changes the original array and returns the new length */
colors = [๐ฃ,๐ก,๐ ,๐ด];
colors.unshift(๐ข);
result (colors):
[๐ข,๐ฃ,๐ก,๐ ,๐ด]
return (length):
5
/* 7 ways to loop through an array in javascript */
// 1. for loop
colors = [๐ฃ,๐ก,๐ ,๐ด];
let ( for i = 0; i < colors.length; i++ ){
console.log(colors[i]);
}
result (colors):
๐ฃ
๐ก
๐
๐ด
// 2. for...in loop
animals = [๐, ๐, ๐, ๐ฆ, ๐ฟ];
for ( let index in animals ){
console.log(animals[index]);
}
result (animals):
๐
๐
๐
๐ฆ
๐ฟ
// 3. while loop
let cars = [๐, ๐, ๐, ๐];
let i = 0;
while ( i < cars.length){
console.log(cars[i]);
i++;
}
result (cars):
๐
๐
๐
๐
// 4. do...while loop
veggies = [๐,๐ถ,๐ฝ๏ธ];
let i = 0;
do {
console.log(veggies[i]);
i++;
} while (i < 5);
result (veggies):
๐
๐ถ
๐ฝ
// 5. for...of loop
tools = [๐ช,๐ช,๐,๐ช,๐จ];
for ( let value of tools ){
console.log(value);
}
result (tools):
๐ช
๐ช
๐
๐ช
๐จ
// 6. forEach method
emotes = [๐,๐,๐ฅน,๐คฃ];
emotes.forEach((value)=>{
console.log(value);
});
result (tools):
๐
๐
๐ฅน
๐คฃ
// 7. map() method
hats = [๐ฉ,๐,๐,โ,๐งข];
const newHats = hats.map((value) => {
return value;
});
console.log(newHats);
result (newHats):
[๐ฉ,๐,๐,โ,๐งข]