|
| 1 | +function filter(arr, callback) { |
| 2 | + const newArray = []; |
| 3 | + for (let i = 0; i < arr.length; i++) { |
| 4 | + if (callback(arr[i], i, arr)) { |
| 5 | + newArray.push(arr[i]) |
| 6 | + } |
| 7 | + } |
| 8 | + return newArray; |
| 9 | +} |
| 10 | + |
| 11 | +function filterObject(object, callback) { |
| 12 | + let newOject = {}; |
| 13 | + for (const key in object) { |
| 14 | + if (callback(object[key], key, object)) { |
| 15 | + newOject = { ...newOject, ...{ [key]: object[key] } }; |
| 16 | + } |
| 17 | + |
| 18 | + } |
| 19 | + return newOject; |
| 20 | +} |
| 21 | + |
| 22 | +// const objFilter = { a: 2, b: 3, c: 4, f: 10 } |
| 23 | +// console.log(filterObject(objFilter, (value => value > 3))); |
| 24 | +// const arr = [1, 2, 2, 3, 4, 5, 6] |
| 25 | +// console.log(filter(arr, (x => x > 3))) |
| 26 | +// filter(arr, ((x, i, a) => { |
| 27 | +// console.log(i); |
| 28 | +// })) |
| 29 | + |
| 30 | + |
| 31 | +function map(array, callback) { |
| 32 | + const newArray = []; |
| 33 | + for (let i = 0; i < array.length; i++) { |
| 34 | + newArray.push(callback(array[i], i, array)) |
| 35 | + } |
| 36 | + return newArray; |
| 37 | +} |
| 38 | + |
| 39 | +function objectMap(object, callback) { |
| 40 | + let newOject = {} |
| 41 | + for (const key in object) { |
| 42 | + newOject = { ...newOject, ...{ [key]: callback(object[key], key) } } |
| 43 | + } |
| 44 | + return newOject; |
| 45 | +} |
| 46 | +// const array = [1, 2, 3, 4] |
| 47 | +// const array = [{ x: 2, y: 3 }, { x: 2, y: 3 }] |
| 48 | +// console.log(map(array, (x => x.x))); |
| 49 | + |
| 50 | +// const obj = { a: 2, b: 3, x: 4 } |
| 51 | +// console.log(Object.keys(obj)); |
| 52 | +// console.log( |
| 53 | +// objectMap(obj, ((val, key) => { |
| 54 | +// return val * 2; |
| 55 | +// } |
| 56 | +// )) |
| 57 | +// ); |
| 58 | + |
| 59 | + |
| 60 | +function reduce(array, callback, initialValue) { |
| 61 | + let accumulator = initialValue ?? 0; |
| 62 | + for (let i = 0; i < array.length; i++) { |
| 63 | + accumulator = callback(accumulator, array[i], i, array) |
| 64 | + } |
| 65 | + return accumulator; |
| 66 | +} |
| 67 | + |
| 68 | +function objectReduce(object, callback, initialValue) { |
| 69 | + let accumulator = initialValue ?? 0; |
| 70 | + for (const key in object) { |
| 71 | + accumulator = callback(accumulator, object[key], key, object); |
| 72 | + } |
| 73 | + return accumulator; |
| 74 | +} |
| 75 | + |
| 76 | +// const array = [1, 2, 3, 4] |
| 77 | +const object = { a: 2, b: 3, x: 4 } |
| 78 | +// console.log(reduce(array, ((acc, item) => { |
| 79 | +// acc.push() |
| 80 | +// }), [] )); |
| 81 | + |
| 82 | +// console.log( |
| 83 | +// objectReduce(object, ((acc, value, key) => { |
| 84 | +// acc += value; |
| 85 | + |
| 86 | +// return acc; |
| 87 | +// }), 0) |
| 88 | +// ); |
| 89 | + |
| 90 | +// function flat(array, depth = 1) { |
| 91 | +// return array.reduce((acc, item) => { |
| 92 | +// if (Array.isArray(item)) { |
| 93 | +// if (depth >= 1) { |
| 94 | +// acc.push(...flat(item, depth - 1)) |
| 95 | +// return acc; |
| 96 | +// } |
| 97 | +// } |
| 98 | +// acc.push(item) |
| 99 | +// return acc; |
| 100 | +// }, []) |
| 101 | +// } |
| 102 | + |
| 103 | +function flat(array, depth = 1) { |
| 104 | + return array.reduce((acc, item) => { |
| 105 | + if (Array.isArray(item) && depth >= 1) { |
| 106 | + acc.push(...flat(item, depth - 1)) |
| 107 | + return acc; |
| 108 | + } |
| 109 | + acc.push(item); |
| 110 | + return acc; |
| 111 | + }, []) |
| 112 | +} |
| 113 | + |
| 114 | +const arr = [[1, 2], 3, [8, 4, [5, 6]]]; |
| 115 | +console.log(flat(arr)); |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + |
0 commit comments