Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //string
- //array loop
- //array
- //most used
- //Most looping system
- //object
- //for in
- //--------array convertion (array loop)
- //for
- //while loop
- //forEach
- //map
- // return array
- // work on each element
- //don't mutate original array
- //must be returned from inside
- // const arrayMultiply = nums.map(num => num * 2)
- // console.log(arrayMultiply)
- //find
- // return single element
- // work on each element
- //don't mutate original array
- //must be returned from inside
- // const result = nums.find(num => num % 2 !== 0 )
- // console.log(result)
- // const nums = [1, 3, 5, 7, 10]
- // filter
- // return array [element] based on condition
- // work on each element
- // don't mutate original array
- // must be returned from inside
- // const result = nums.filter(num => num % 2 == 0)
- // console.log(result)
- // const nums = [ 1,3, 5, 7]
- //some
- // return true/false based on condition
- // work on each element
- //don't mutate original array
- //must be returned from inside
- // const result = nums.some(num => num % 2 == 0)
- // console.log(result)
- //const nums = [1, 3, 5, 7]
- //every
- // return true/false based on condition
- // work on each element
- //don't mutate original array
- //must be returned from inside
- // const result = nums.every(num => num % 2 !== 0)
- // console.log(result)
- // const nums = [0, 3, 5, 7]
- // findIndex
- // return index number based on condition
- // work on each element
- // don't mutate original array
- // must be returned from inside
- // const result = nums.findIndex(num => num % 2 !== 0)
- // console.log(result)
- // reduce(reducing array element)
- // return based on criteria
- // work on each element
- // don't mutate original array
- // must be returned from inside
- // const nums = [0, 3, 5, 7]
- // acc - 0 curr-3 result - 3
- // acc - 3 curr-5 result - 8
- // acc - 8 curr-7 result - 15
- // with default value
- // acc - 5 curr-0 result - 5
- // acc - 5 curr-3 result - 8
- // acc - 8 curr-5 result - 13
- // acc - 13 curr-7 result -20
- // const result = nums.reduce((acc, curr, index, array) => acc + curr, 5)
- // console.log(result)
- // const nums = {
- // value1: 1,
- // value2: 2,
- // value3: 3
- // }
- // console.log(Object.keys(nums))
- // console.log(Object.entries(nums))
- // let total = 0
- // //object looping
- // for (let [key, value] of Object.entries(nums)) {
- // // total = total + value
- // //console.log(num[1])
- // // console.log(nums[num])
- // }
- // console.log(total)
- // function summation(){
- // //for of
- // let total = 4
- // for (let num of nums){
- // total += num
- // }
- // return total
- // }
- //console.log(summation())
- //for in(object, array-not recommended)
- //array helper - map, reduce, find, findIndex, filter, some , every
- // let kvArray = [{ key: 1, value: 10 },
- // { key: 2, value: 20 },
- // { key: 3, value: 30 }]
- // let reformattedArray = kvArray.map(obj => {
- // let rObj = {}
- // rObj[obj.key] = obj.value
- // return rObj
- // })
- // reformattedArray is now [{1: 10}, {2: 20}, {3: 30}],
- // let people = [
- // { name: 'Alice', age: 21 },
- // { name: 'Max', age: 20 },
- // { name: 'Jane', age: 20 }
- // ];
- // function groupBy(objectArray, property) {
- // return objectArray.reduce(function (acc, obj) {
- // let key = obj[property]
- // if (!acc[key]) {
- // acc[key] = []
- // }
- // acc[key].push(obj)
- // return acc
- // }, {})
- // }
- // let groupedPeople = groupBy(people, 'age')
- // groupedPeople is:
- // {
- // 20: [
- // { name: 'Max', age: 20 },
- // { name: 'Jane', age: 20 }
- // ],
- // 21: [{ name: 'Alice', age: 21 }]
- // }
- // let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice']
- // let countedNames = names.reduce(function (allNames, name) {
- // if (name in allNames) {
- // allNames[name]++
- // }
- // else {
- // allNames[name] = 1
- // }
- // return allNames
- // }, {})
- // countedNames is:
- // { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }
- // let myArray = ['a', 'b', 'a', 'b', 'c', 'e', 'e', 'c', 'd', 'd', 'd', 'd']
- // let myOrderedArray = myArray.reduce(function (accumulator, currentValue) {
- // if (accumulator.indexOf(currentValue) === -1) {
- // accumulator.push(currentValue)
- // }
- // return accumulator
- // }, [])
- // console.log(myOrderedArray)
- //0 +1 = 1
- //1 + 2 = 3
- //3 + 3 = 6
- // let sum = [{ x: 1 }, { x: 2 }, { x: 3 }].reduce(function (accumulator, currentValue) {
- // console.log(accumulator, currentValue)
- // return accumulator.x + currentValue.x
- // })
- // console.log(sum)
- // let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice']
- // let countedNames = names.reduce(function (allNames, name) {
- // console.log(allNames, name)
- // if(allNames[name]){
- // allNames[name] ++
- // }else{
- // allNames[name] = 1
- // }
- // return allNames
- // }, {})
- // console.log(countedNames)
- // // { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }
- // const obj = {
- // alice: 1,
- // alice: 1
- // }
- // console.log('alice' in obj )
- let friends = [
- {
- name: 'Anna',
- books: ['Bible', 'Harry Potter'],
- age: 21
- },
- {
- name: 'Bob',
- books: ['War and peace', 'Romeo and Juliet'],
- age: 26
- },
- {
- name: 'Alice',
- books: ['The Lord of the Rings', 'The Shining'],
- age: 18
- }
- ]
- // allbooks - list which will contain all friends' books +
- // additional list contained in initialValue
- let allbooks = friends.reduce(function (accumulator, currentValue) {
- console.log(accumulator, currentValue)
- return [...accumulator, ...currentValue.books]
- }, [])
- console.log(allbooks)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement