Using splice to add items in any position
const arr = ['a','b','c','d']
x = arr.splice(2,0,'3')
x will be empty - []
arr will be ["a", "b", "3",
"c", "d"]
Add to the beginning of array
arr.unshift(-13)
[-13, "a", "b", "3",
"c", "d"]
remove 1st item
arr.shift()
["a", "b", "3", "c",
"d"]
For Each
var temp =””
arr.forEach(function (item) {
if (isNaN(item)) {
temp += item;
}
});
temp = "abcd"
mapping
const test
=[{name:"herby",cash:23},{name:"boris",cash:456} ]
const cash = test.map(x=>x.cash)
cash = [23, 456]
Every and Some
const test2
=[{name:"herby",cash:23,eyecolor:"brown"},{name:"boris",cash:456}
]
test2.some(x=>x.eyecolor)
true
test2.every(x=>x.eyecolor)
false
Find
returns 1st item
const z = test2.find(x=>{return x.eyecolor ===
"brown"})
z {name:"herby",cash:23,eyecolor:"brown"}
const w = test2.find(x=>{return x.cash > 0})
w {name:"herby",cash:23,eyecolor:"brown"}
to get multiple items as an array use:
Filter
const t = test2.filter(x=>{return x.cash > 0})
sort
ascending
test2.sort((x,z)=>x.cash -z.cash)
descending
test2.sort((x,z)=>z.cash -x.cash)
Aggregating
console.log(test2.reduce((a,c)=>a+=c.cash,0))
479