arrays Flashcards
includes
let b=[10, 20, 30, 40]
b.includes(10)
=> true
map
b.map(one=>one+10)
=> [20, 30, 40, 50]
reduce
plus
let b=[10, 20, 30, 40]
b.reduce((one,two)=>one+two,0)
=> 100
let b = [10, 20, 30, 40]
b.reduce((one, two) => one * two, 1)
=> 2400
reduce #order
let a = [100, 200, 400, 100, 200];
let b = a.reduce((one, two) => {
if (one[two]) {
one[two] = one[two] + 1;
} else {
one[two] = 1;
}
return one;
}, {});
console.log(b);
=> { ‘100’: 2, ‘200’: 2, ‘400’: 1 }
remove duplicates
const one = [2, 1, 3, 5, 3, 2];
let two = […new Set(one)]
two # [ 2, 1, 3, 5 ]
sort #ordenar
descendente
const one = [2, 1, 3, 5, 3, 2];
const two = one.sort() THE SAME const two = one.sort((a-b)=>a-b)
const two = one.sort() THE SAME const two = one.sort((a-b)=>a-b)
array.findIndex(CALLBACK) // array.find(CALLBACK)
=> 3
array.indexof(2)
=>3
they are the same
both return only a value
const a = [2, 1, 3, 5, 3, 2];
const b = a.forEach((one, two, three) => {
console.log(one, “=”, two, “=”, three);
});
2 = 0 = [ 2, 1, 3, 5, 3, 2 ]
1 = 1 = [ 2, 1, 3, 5, 3, 2 ]
3 = 2 = [ 2, 1, 3, 5, 3, 2 ]
5 = 3 = [ 2, 1, 3, 5, 3, 2 ]
3 = 4 = [ 2, 1, 3, 5, 3, 2 ]
2 = 5 = [ 2, 1, 3, 5, 3, 2 ]
convert // text => array // split
const a=”sadkljsaldkñjf”
const b= a.split(“”)
console.log(b);
[
‘s’, ‘a’, ‘d’, ‘k’,
‘l’, ‘j’, ‘s’, ‘a’,
‘l’, ‘d’, ‘k’, ‘ñ’,
‘j’, ‘f’
]
split // second example
const a=”sadkljsaldkñjf”
=> a
[“sadkljsaldkñjf”]
condición ? expresión1 : expresión2
const cnt = original.length > modified.length ? original : modified;
reverse // split
const a =[100,200,300]
const b=a.reverse()
=> [ 300, 200, 100 ]
join
example1
const a = [“one”,”two”,”three”]
const b = a.join(“-“);
=> one-two-three // string
const a = [“one”,”two”,”three”]
const b = a.join();
=> one,two,three // string
match // string.match(regex)
let text = “El gato se sube al árbol.”;
let result = text.match(/gato/);
console.log(result); // Output: [“gato”]
result = text.match(/perro/);
console.log(result); // Output: null