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
| let testObj = { a: { b: { c: "get it", d: [{}, { e: "get it" }] } } };
let logLine = title => console.log(`---------------------------${title}---------------------------`);
logLine("loop"); console.log( getWithLoop(testObj, ["a", "b", "d", "1", "e"], 2), getWithLoop(testObj, "a.b.d[1].e", 2), "get it" ); console.log( getWithLoop(testObj, ["a", "b", "d", "0", "e"], "default"), getWithLoop(testObj, "a.b.d[0].e", "default"), "default" ); console.log( getWithLoop(testObj, ["a", "b", "d", "0", "e"]), getWithLoop(testObj, "a.b.d[0].e"), undefined ); logLine("loop");
logLine("reduce"); console.log( getWithReduce(testObj, ["a", "b", "d", "1", "e"], 2), getWithReduce(testObj, "a.b.d[1].e", 2), "get it" ); console.log( getWithReduce(testObj, ["a", "b", "d", "0", "e"], "default"), getWithReduce(testObj, "a.b.d[0].e", "default"), "default" ); console.log( getWithReduce(testObj, ["a", "b", "d", "0", "e"]), getWithReduce(testObj, "a.b.d[0].e"), undefined ); logLine("reduce");
logLine("Recursive"); console.log( getWithRecursive(testObj, ["a", "b", "d", "1", "e"], 2), getWithRecursive(testObj, "a.b.d[1].e", 2), "get it" ); console.log( getWithRecursive(testObj, ["a", "b", "d", "0", "e"], "default"), getWithRecursive(testObj, "a.b.d[0].e", "default"), "default" ); console.log( getWithRecursive(testObj, ["a", "b", "d", "0", "e"]), getWithRecursive(testObj, "a.b.d[0].e"), undefined ); logLine("Recursive");
|