Skip to content

Instantly share code, notes, and snippets.

@kentcdodds
Created August 29, 2018 17:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kentcdodds/da83207bc5188f974302a3fb5c8636bd to your computer and use it in GitHub Desktop.
Save kentcdodds/da83207bc5188f974302a3fb5c8636bd to your computer and use it in GitHub Desktop.
// .filter(Boolean)
// .fitler to unique values
const fruit = ['Apple', 'Banana', 'Apple', 'Orange']
console.log(Array.from(new Set(fruit)))
const unique = (element, index, fullArray) => fullArray.indexOf(element) === index
console.log(fruit.filter(unique))
/*
const prod = false
const fruit = [
'Apple', 'Banana', 'Orange',
'Pear', 'Peach',
prod ? 'Grape' : null
].filter(Boolean)
console.log(fruit)
/*
console.log(
fruit.reduce((acc, element) => {
return `${acc}${element}`
})
)
const pOnlyFruitCapReduce = fruit.reduce((acc, element) => {
if (element.startsWith('P')) {
acc.push(element.toUpperCase())
}
return acc
}, [])
console.log(pOnlyFruitCapReduce)
const pOnlyFruitCapArray = fruit
.filter(element => element.startsWith('P'))
.map(element => element.toUpperCase())
console.log(pOnlyFruitCapArray)
let pOnlyFruitCap = []
for (let index = 0; index < fruit.length; index++) {
const element = fruit[index]
if (element.startsWith('P')) {
pOnlyFruitCap[pOnlyFruitCap.length] = element.toUpperCase()
}
}
console.log(pOnlyFruitCap)
/*
let pOnlyFruitWithFilter = fruit.filter(element => element.startsWith('P'))
console.log(pOnlyFruitWithFilter)
let pOnlyFruit = []
for (let index = 0; index < fruit.length; index++) {
const element = fruit[index];
if (element.startsWith('P')) {
pOnlyFruit[pOnlyFruit.length] = element
}
}
console.log(pOnlyFruit)
*/
/*
const allCapsFruitWithMap = fruit.map(element => {
return element.toUpperCase()
})
console.log(allCapsFruitWithMap)
let allCapsFruit = []
for (let index = 0; index < fruit.length; index++) {
const element = fruit[index];
allCapsFruit[index] = element.toUpperCase()
}
console.log(allCapsFruit)
console.log(fruit)
*/
/*
fruit.forEach(element => {
console.log(element)
})
for (let index = 0; index < fruit.length; index++) {
const element = fruit[index];
console.log(element)
}
*/
// console.log(fruit.reverse())
// console.log(fruit)
// let newFruit = []
// for (let index = fruit.length - 1; index >= 0; index--) {
// newFruit[newFruit.length] = fruit[index];
// }
// for (let index = 0; index < newFruit.length; index++) {
// fruit[index] = newFruit[index];
// }
// console.log(fruit)
/*
const obj = {name: 'bob'}
const fruit = ['Apple', obj, 'Banana', 'Apple', 'Orange']
console.log(fruit.lastIndexOf('Apple'))
let lastIndexOfApple
for (let index = fruit.length - 1; index >= 0; index--) {
const element = fruit[index];
if (element === 'Apple') {
lastIndexOfApple = index
break;
}
}
console.log(lastIndexOfApple)
console.log(fruit)
console.log(fruit.slice(-2))
console.log(fruit.fill(3, 2, 5))
console.log(
Array.from({length: 2}, (v, i) => i)
)
console.log(Array.of(8,3))
console.log(Array.from({'0': 'hi', length: 1}))
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment