var empty = []
var emojis = [
"🍺",
"🍻",
"🍶",
"🍵",
"☕️",
"🍼",
"💻",
"👙",
"🐶",
"🎮",
"💪"
]
var users = [
{
"id": 1000,
"name": "Corey Griffith",
"username": "coGriffith",
"gender": "male",
"age": 13
},
{
"id": 1001,
"name": "Marion McDaniel",
"username": "mMcDaniel",
"gender": "female",
"age": 15
},
{
"id": 1002,
"name": "Tom Robbins",
"username": "tRobbins",
"gender": "male",
"age": 15
},
{
"id": 1003,
"name": "Ruth Harvey",
"username": "rHarvey",
"gender": "female",
"age": 14
},
{
"id": 1004,
"name": "Terry Willis",
"username": "tWillis",
"gender": "male",
"age": 13
}
]
var products = [
{
"id": "001",
"name": "iPad Pro",
"price": 30900,
"storage": 32,
"type": "iPad",
"size": "12.9"
},
{
"id": "002",
"name": "iPad Pro",
"price": 34900,
"storage": 128,
"type": "iPad",
"size": "12.9"
},
{
"id": "003",
"name": "iPad Pro",
"price": 38900,
"storage": 256,
"type": "iPad",
"size": "12.9"
},
{
"id": "004",
"name": "iPad Pro",
"price": 22900,
"storage": 32,
"type": "iPad",
"size": "9.7"
},
{
"id": "005",
"name": "iPad Pro",
"price": 26900,
"storage": 128,
"type": "iPad",
"size": "9.7"
},
{
"id": "006",
"name": "iPad Pro",
"price": 30900,
"storage": 256,
"type": "iPad",
"size": "9.7"
},
{
"id": "007",
"name": "iPad Air 2",
"price": 14400,
"storage": 32,
"type": "iPad",
"size": "9.7"
},
{
"id": "008",
"name": "iPad Air 2",
"price": 18400,
"storage": 128,
"type": "iPad",
"size": "9.7"
},
{
"id": "009",
"name": "iPad mini 4",
"price": 14400,
"storage": 32,
"type": "iPad",
"size": "7.9"
},
{
"id": "010",
"name": "iPad mini 4",
"price": 18400,
"storage": 128,
"type": "iPad",
"size": "7.9"
},
{
"id": "011",
"name": "iphone7",
"price": 26000,
"storage": 32,
"type": "iPhone",
"size": "4.7"
},
{
"id": "012",
"name": "iphone7",
"price": 55000,
"storage": 128,
"type": "iPhone",
"size": "4.7"
},
{
"id": "013",
"name": "iphone7Plus",
"price": 49000,
"storage": 32,
"type": "iPhone",
"size": "5.5"
},
{
"id": "014",
"name": "iphone7Plus",
"price": 59000,
"storage": 128,
"type": "iPhone",
"size": "5.5"
},
{
"id": "020",
"name": "iphoneX",
"price": 69000,
"storage": 128,
"type": "iPhone",
"size": "6.0"
},
{
"id": "017",
"name": "iphone8",
"price": 60000,
"storage": 128,
"type": "iPhone",
"size": "6.0"
}
]
Creates a new array with all elements that pass the test implemented by the provided function. More
/* Try change the code */
emojis.filter(
emoji => emoji === "🐶")
/* and press ▶ Run below */
emojis.filter(
emoji => emoji >= "🐶")
users.filter(
user => user.age > 13)
users.filter(
user => user.gender === "female")
products.filter(
product => product.price < 20000)
products.filter(
product => product.price > 20000
&& product.price < 30000)
Returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned. More
emojis.find(
emoji => emoji === "🐶")
users.find(
user => user.age > 13)
products.find(
product => product.price < 20000)
Returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned. More
emojis.findIndex(
emoji => emoji === "🐶")
users.findIndex(
user => user.age > 13)
products.findIndex(
product => product.price < 20000)
Creates a new array with the results of calling a provided function on every element in the calling array. More
emojis.map(
emoji =>
'Char code of ' + emoji + ' is ' + emoji.codePointAt(0))
users.map(
user => user)
users.map(
user => user.gender)
users.map(
user =>
user.name + ' age ' + user.age)
users.map(
user => user.gender).length
Applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value. More
users.reduce(
(previousValue, currentValue) =>
previousValue + currentValue.age, 0)
users.reduce(
/* Try change params */
(sumAge, user) =>
sumAge + user.age, 0)
emojis.reduce(
(result, emoji) =>
result + '💥' + emoji, "")
products.reduce(
(sumPrice, product) =>
sumPrice + product.price, 0)
Two method can put together.
users.filter(
user =>
user.gender === 'female')
.map(user => user.username)
Sorts the elements of an array and returns the array. We can define the conditions for sorting. More
emojis.sort()
users.sort()
/*Order by name*/
users.sort(
(a, b) => (a.name - b.name))
/*Order by price*/
products.sort(
(a, b) => (a.price - b.price))
Reverses an array in place. More
emojis.reverse()
users.reverse()
products.reverse()
Adds one or more elements to the end of an array and returns the new length of the array. More
emojis.push('💥')
emojis.push('⚽', '🏀')
/* Observe this carefully */
emojis.push(['🔫', '💣', '🔪'])
We can pure push too.
// Use spread operator
var newEmojis = [emojis, '🏀']
// Try push to array
newEmojis.push('🏀')
newEmojis
// or use concat
var newEmojisAgain = [].concat(emojis, '👍')
// Try push to array
newEmojisAgain.push('🏀')
newEmojisAgain
Removes the first element from an array and returns that removed element. This method changes the length of the array. More
emojis.shift()
users.shift()
products.shift()
Removes the last element from an array and returns that element. This method changes the length of the array. More
emojis.pop()
users.pop()
products.pop()
Changes the contents of an array by removing existing elements and/or adding new elements. More
/*Add new element to index 2*/
emojis.splice(2, 0,
"👍", "👊", "✊", "✌️", "👌", "✋")
/*Add new element*/
/*to index 2 - 4*/
emojis.splice(2, 3,
"👍", "👊", "✊")
/*Remove element index 2 - 3*/
users.splice(2, 2)
Joins all elements of an array (or an array-like object) into a string and returns this string. More
emojis.join()
emojis.join("|")
This method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. More
//Take first array and concatenate
/* with the second array.*/
emojis.concat(users)
//Concatenate more than 2 arrays
empty.concat(users, emojis)
//Concatenate more than 3 elements
//with empty array
[].concat(["🇹🇭", "🇰🇷", "🇯🇵"], emojis, ["🇹🇭", "🇰🇷", "🇯🇵"])
Returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified. More
// Extracts all element.
emojis.slice()
// Extracts the third element.
// through the last element.
emojis.slice(3)
// Extracts the third element.
// through the fourth element.
emojis.slice(3, 4)
Returns a string representing the specified array and its elements. More
// Return toString value of emojis.
emojis.toString()
pure Copy Array
// Copy emojis array
var copyEmojis = [emojis]
// Try push element to emojis
copyEmojis.push('🏀')
copyEmojis
// or use [].concat
var copyEmojisAgain = [].concat(emojis)
// Try push element to emojis
copyEmojisAgain.push('🏀')
copyEmojisAgain
Shallow copies part of an array to another location in the same array and returns it, without modifying its size. More
// place at position 0
// the element between
// position 3 and 4
var copyEmojis = emojis.slice(0)
copyEmojis.copyWithin(0, 3, 4)
// place at position 1
// the elements after
// position 3
var copyEmojis = emojis.slice(0)
copyEmojis.copyWithin(1, 3)
Returns a new Array Iterator object that contains the key/value pairs for each index in the array. More
// place at position 0
var iterator = emojis.entries()
iterator.next().value
Tests whether all elements in the array pass the test implemented by the provided function. More
// function to test
function isPriceBelowThreshold(product) {
return product.price < 100000;
}
products.every(isPriceBelowThreshold)
Fills all the elements of an array from a start index to an end index with a static value. The end index is not included. More
var copyEmojis = emojis.slice(0)
// fill with 0 from position
// 2 until position 4
copyEmojis.fill(0, 2, 4)
var copyEmojis = emojis.slice(0)
// fill with 1 from position 2
copyEmojis.fill(1, 2)
var copyEmojis = emojis.slice(0)
// fill with 2
copyEmojis.fill(2)
Returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned. More
// Find the first user
// age more than 14
users.find(function(user) {
return user.age > 14
})
Executes a provided function once for each array element. More
let copyEmojis = ""
emojis.forEach(function(emoji) {
copyEmojis += emoji
})
copyEmojis
Determines whether an array includes a certain element, returning true or false as appropriate. It uses the sameValueZero algorithm to determine whether the given element is found. More
emojis.includes("💻")
emojis.includes("🇹🇭")
emojis.includes("🍵")
Creates a new, shallow-copied Array instance from an array-like or iterable object. More
Array.from("shiba")
Creates a new Array instance with a variable number of arguments, regardless of number or type of the arguments. More
Array.of(1, 2, 3, 4, 5)
Returns the first index at which a given element can be found in the array, or -1 if it is not present. More
emojis.indexOf("☕️")
emojis.indexOf("👙")
emojis.indexOf("🍶")
Returns a new Array Iterator object that contains the keys for each index in the array. More
let copyEmoji = ""
var iterator = emojis.keys()
for (let key of iterator) {
copyEmoji += key; // expected output: 0 1 2
}
copyEmoji
Returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex. More
emojis.lastIndexOf("🍻")
emojis.lastIndexOf("👙")
emojis.lastIndexOf("💪")
Creates a new array with the results of calling a provided function on every element in the calling array. More
var array = [1, 4, 9, 16]
array.map(x => x * 2)
Applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value. More
[[0, 1], [2, 3], [4, 5]].reduceRight(
(previousValue, currentValue) => previousValue.concat(currentValue)
)
Tests whether at least one element in the array passes the test implemented by the provided function. More
var array = [1, 2, 3, 4, 5]
var even = function(element) {
// checks whether an element is even
return element % 2 === 0;
}
array.some(even)
Returns a string representing the elements of the array. The elements are converted to Strings using their toLocaleString methods and these Strings are separated by a locale-specific String (such as a comma “,”). More
var array1 = [1, "a", new Date("21 Dec 1997 14:12:00 UTC")]
array1.toLocaleString("en", {timeZone: "UTC"})
Adds one or more elements to the beginning of an array and returns the new length of the array. More
emojis.unshift("🍺","🍻")
Returns a new Array Iterator object that contains the values for each index in the array. More
let copyArray = ""
const array = ["a", "b", "c"]
const iterator = array.values()
for (const value of iterator) {
copyArray += value //
}
copyArray
Property is the same function object as the initial value of the values() property. More
let copyArray = ""
var arr = ["w", "y", "k", "o", "p"]
var eArr = arr[Symbol.iterator]()
for (let letter of eArr) {
copyArray += letter
}
copyArray
"output"