GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — javascript-coding/array-prototype.js at main · mavrix-builds/javascript-coding Expand file tree/ Copy patharray-prototype.jsMore file actions 124 lines (99 loc) · 2.76 KB / Copy patharray-prototype.jsFile metadata and controls124 lines (99 loc) · 2.76 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 function filter(arr, callback) { const newArray = []; for (let i = 0; i < arr.length; i++) { if (callback(arr[i], i, arr)) { newArray.push(arr[i]) } } return newArray; } function filterObject(object, callback) { let newOject = {}; for (const key in object) { if (callback(object[key], key, object)) { newOject = { ...newOject, ...{ [key]: object[key] } }; } } return newOject; } // const objFilter = { a: 2, b: 3, c: 4, f: 10 } // console.log(filterObject(objFilter, (value => value > 3))); // const arr = [1, 2, 2, 3, 4, 5, 6] // console.log(filter(arr, (x => x > 3))) // filter(arr, ((x, i, a) => { // console.log(i); // })) function map(array, callback) { const newArray = []; for (let i = 0; i < array.length; i++) { newArray.push(callback(array[i], i, array)) } return newArray; } function objectMap(object, callback) { let newOject = {} for (const key in object) { newOject = { ...newOject, ...{ [key]: callback(object[key], key) } } } return newOject; } // const array = [1, 2, 3, 4] // const array = [{ x: 2, y: 3 }, { x: 2, y: 3 }] // console.log(map(array, (x => x.x))); // const obj = { a: 2, b: 3, x: 4 } // console.log(Object.keys(obj)); // console.log( // objectMap(obj, ((val, key) => { // return val * 2; // } // )) // ); function reduce(array, callback, initialValue) { let accumulator = initialValue ?? 0; for (let i = 0; i < array.length; i++) { accumulator = callback(accumulator, array[i], i, array) } return accumulator; } function objectReduce(object, callback, initialValue) { let accumulator = initialValue ?? 0; for (const key in object) { accumulator = callback(accumulator, object[key], key, object); } return accumulator; } // const array = [1, 2, 3, 4] const object = { a: 2, b: 3, x: 4 } // console.log(reduce(array, ((acc, item) => { // acc.push() // }), [] )); // console.log( // objectReduce(object, ((acc, value, key) => { // acc += value; // return acc; // }), 0) // ); // function flat(array, depth = 1) { // return array.reduce((acc, item) => { // if (Array.isArray(item)) { // if (depth >= 1) { // acc.push(...flat(item, depth - 1)) // return acc; // } // } // acc.push(item) // return acc; // }, []) // } function flat(array, depth = 1) { return array.reduce((acc, item) => { if (Array.isArray(item) && depth >= 1) { acc.push(...flat(item, depth - 1)) return acc; } acc.push(item); return acc; }, []) } const arr = [[1, 2], 3, [8, 4, [5, 6]]]; console.log(flat(arr)); |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |