Array & Object methods
Array
- No mutate
- Mutate the array
// Map: Creating a NEW array containing output of some processing done on each element of the array.
const newNumbers = arr.map((element, index) => {
element += index
})
const cc = [1, 2].concat([3, 4]) // [1, 2, 3, 4]. No mutate
const sl = cc.slice(1, -1) // [2, 3] (ko lấy end index); cũng có thể dùng .slice() để shallow copy
// Filters, every, some - Same arguments: .filter((value, index, array)=>{...})
const f = numbers.filter((n) => n > 5) // Returns new array
const allPositive = numbers.every((value) => value >= 0) // true
const atLeastOneNegative = numbers.some((value) => value <= 0) // false
// Joining & Spliting
const joined = [1, 2, 3].join('+') // Return string "1+2+3"
const split = joined.split('+') // String method: Về lại thành [1, 2, 3]
// Finding PRIMITIVE Elements
numbers.indexOf(4, 0) // (value, fromIndex). Nếu ko có `value` trong arr -> Return -1
numbers.lastIndexOf('a', 2) // (value, fromIndex).
numbers.includes(1, 2) // (value, fromIndex). Returns boolean
numbers.find((value) => value > 2) // Returns the value of the FIRST element ok with the callback
numbers.findIndex((value) => value > 2) // Returns the index of the FIRST element ok with the callback
// Set
const newSet = new Set(someArray) // methods: .add(), .delete(), .has(), .clear(), .size
const uniqueArray = [...new Set(someArray)] // For primitive values
const uniqueArrayOfObjects = Array.from(new Set(arr.map(JSON.stringify))).map(
JSON.parse,
)
// Reduce
// first call: previousValue = 15, currentValue = 16, currentIndex = 1
// second call: previousValue = 31, currentValue = 17, currentIndex = 2
const summ = [15, 16, 17].reduce(
(previousValue, currentValue, currentIndex) => {
return previousValue + currentValue
},
initialValue,
)
// Flat
var arr2 = [1, 2, [3, 4, [5, 6]]]
arr2.flat() // [1, 2, 3, 4, [5, 6]]
arr2.flat(2) // [1, 2, 3, 4, 5, 6]
const numbers = [3, 4]
numbers.push(5, 6) // Thêm vào cuối
numbers.pop() // Xóa 1 số cuối
numbers.unshift(1, 2) // Thêm vào đầu
numbers.shift() // Xóa 1 số đầu
numbers.reverse() // Đảo ngược
numbers.splice(2, 1, 'a', 'b') // (fromIndex, deleteCount, item1, item2, ...); returns an array containing the deleted items
numbers.sort((a, b) => a - b) // Sort theo `compare fn`: Nếu returns < 0 thì a xếp trước b, 0 thì giữ nguyên
// forEach: Executes a provided function once per array element. Returns `undefined`
numbers.forEach((element, index) => {
element.savingToDatabase()
})
Object
var obj = { a: 1, b: 2 }
// Cả 3 đều trả về Array -> Thường chain tiếp với `map`
Object.keys<string>(obj) // ['a', 'b']
Object.values<number>(obj) // [1, 2]
Object.entries<number>(obj) // [['a', 1], ['b', 2]]
// Object.assign: Source overwrites target
const target = { a: 1, b: 2 }
const source = { b: 4, c: 5 }
const returnedTarget = Object.assign(target, source) // assign to `target` from `source`
console.log(returnedTarget) // { a: 1, b: 4, c: 5 }
console.log(target) // { a: 1, b: 4, c: 5 }
console.log(source) // { b: 4, c: 5 };