javascript 硬结构中的 reduce 和 memoisation 问题

problem with reduce and memoisation in javascript hardstructure

我必须完成一项任务,我需要使用嵌套函数数组对象和其他数据类型迭代硬结构。将字符串中存在或不存在的所有数字放入数组中,然后使用 reduce 方法我需要计算它们的总和,在计算它们的总和后我需要对此函数进行记忆。但是有些东西对我的 reduce 不起作用,即使我已经写了最简单的任务,计算数字的总和。我的 reduce 并没有真正计算出正确的方法它跳过了结构的某些部分并且没有收集所有的数字也没有真正计算正确我认为

const ex1 = () => ({ a: 13 });
const ex2 = () => [1, 2, 3, 4, 5, "a,,,,,,", 2, 2, 2];
const ex3 = () => [...ex2()];
const ex4 = new Set([1, 2, 3, 4]);

const hardStructure = [
  {
    a: 1,
    b: "2",
    c: [
      {
        a: "123",
        b: "@333z",
        c: "122,333,4444,5555",
        z: ex4,
        d: [
          [
            123,
            1,
            "1111",
            23323,
            1222,
            55,
            [1212, 1212, 1212],
            {
              a: 111,
              b: null,
              c: () => 111,
              d: 22,
              e: "e",
              f: () => "fffqqq",
            },
          ],
          [
            2212,
            1,
            "111211",
            23,
            121,
            22,
            [33, 3, 3],
            {
              a: 3,
              b: null,
              c: () => 11221,
              d: 2112,
              e: "e11",
              f: () => "fffqqq",
              g: 2,
            },
          ],
          [
            11,
            22,
            "dsds",
            {
              a: {
                b: {
                  c: {
                    d: {
                      a: 1,
                      b: [ex1()],
                      c: {
                        a: {
                          b: {
                            c: {
                              d: {
                                a: new Map([
                                  ["a2134", 2123123],
                                  ["b", 3],
                                  ["c", 3],
                                ]),
                                b: ex2,
                                c: [...ex3(), "1", 1],
                              },
                            },
                          },
                        },
                      },
                    },
                  },
                },
              },
            },
          ],
        ],
      },
    ],
  },
  {
    d: [
      [
        123,
        1,
        "1111",
        23323,
        1222,
        55,
        [121322332, 12132322, 12323212],
        {
          a: 111,
          b: null,
          c: () => 1123231,
          d: 22,
          e: "e",
          f: () => "fffqqq",
        },
      ],
      [
        2212,
        1,
        "111211",
        23,
        121,
        22,
        [33, 3, 3],
        {
          a: 3,
          b: null,
          c: () => 1123221,
          d: 211,
          e: "e1231",
          f: () => "fffqq1232312123q",
          g: 2123,
        },
      ],
      [
        11,
        22,
        "dsds",
        {
          a: {
            b: {
              c: {
                d: {
                  a: 1,
                  b: [ex1()],
                  c: {
                    a: {
                      b: {
                        c: {
                          d: {
                            a: new Map([
                              [true, 222312],
                              [2, 322],
                              ["c2", 32],
                              [() => {}, 32],
                            ]),
                            b: ex2,
                            c: [...ex3(), "1121123", 1],
                          },
                        },
                      },
                    },
                  },
                },
              },
            },
          },
        },
      ],
    ],
  },
  {
    a: {
      b: {
        c: {
          d: {
            a: 112312,
            b: [1],
            c: {
              a: {
                b: {
                  c: {
                    d: {
                      a: "",
                      b: "",
                      c: "",
                      v: "v12312323",
                    },
                  },
                },
              },
            },
          },
        },
      },
    },
  },
];

const numbersArr = [];
const iterate = (hardStructure) => {
  if (hardStructure === null || hardStructure === undefined) {
    return [];
  }
  if (
    hardStructure.constructor === new Set().constructor ||
    hardStructure.constructor === new Map().constructor
  ) {
    console.log("console from map or set");
    hardStructure.forEach((element) => {
      // console.log("123", element);
      return iterate(element);
    });
  }

  if (typeof hardStructure === "function") {
    return iterate(hardStructure());
  }
  if (Array.isArray(hardStructure)) {
    hardStructure.map((items) => {
      return iterate(items);
    });
  }
  if (typeof hardStructure === "object" && !Array.isArray(hardStructure)) {
    for (let key in hardStructure) {
      // console.log(hardStructure[key]);
      return iterate(hardStructure[key]);
    }
  }
  if (typeof hardStructure !== "string") {
    const stringItem = String(hardStructure);
    for (let i = 0; i < stringItem.length; i++) {
      if (!isNaN(+stringItem)) {
        numbersArr.push(+stringItem);
      }
    }
  }

  for (let i = 0; i < hardStructure.length; i++) {
    // console.log(i);
    if (!isNaN(+hardStructure[i])) {
      // console.log("12345678910", isNaN(+hardStructure[2]));
      numbersArr.push(+hardStructure[i]);
    }
  }

  const sumWithInitial = numbersArr.reduce((accumulator, currentValue) => {
    console.log(accumulator,currentValue)
    return accumulator + currentValue;
  },0);

  //  console.log(numbersArr)
  console.log(sumWithInitial);
  return sumWithInitial;
};

iterate(hardStructure);

您的要求并不完全清楚,尤其是在处理 String 和 Function 属性方面。此解决方案假定您想要 调用 函数并且您可以对字符串使用 parseInt。如果您需要更改它,应该清楚如何以及在何处进行更改:

const sumNumbers = (xs) =>
  xs == null
    ? 0
  : xs .constructor == String
    // ? parseInt (xs, 10) || 0 // original version not enough.  Below might not be so either
    ? xs .includes (',') ? sumNumbers (xs .split (',')) : Number (xs .replaceAll (/\D/g, ''))
  : xs .constructor == Number
    ? xs
  : xs .constructor === Array
    ? xs .map (sumNumbers) .reduce ((a, b) => a + b, 0)
  : xs .constructor === Set
    ? sumNumbers ([...xs])
  : xs .constructor === Map
    ? sumNumbers ([...xs .values()])
  : xs .constructor === Function
    ? sumNumbers (xs ()) // or just 0?
  : xs .constructor == Object
    ? sumNumbers (Object .values (xs))
  // TODO: Other types possible here?
    : 0


const ex1 = () => ({a: 13}), 
      ex2 = () => [1, 2, 3, 4, 5, "a,,,,,,", 2, 2, 2], 
      ex3 = () => [...[1, 2, 3, 4, 5, "a,,,,,,", 2, 2, 2]], 
      ex4 = new Set ([1, 2, 3, 4]), 
      hardStructure = [{a: 1, b: "2", c: [{a: "123", b: "@333z", c: "122,333,4444,5555", z: ex4, d: [[123, 1, "1111", 23323, 1222, 55, [1212, 1212, 1212], {a: 111, b: null, c: () => 111, d: 22, e: "e", f: () => "fffqqq"}], [2212, 1, "111211", 23, 121, 22, [33, 3, 3], {a: 3, b: null, c: () => 11221, d: 2112, e: "e11", f: () => "fffqqq", g: 2}], [11, 22, "dsds", {a: {b: {c: {d: {a: 1, b: [{a: 13}], c: {a: {b: {c: {d: {a: new Map ([["a2134", 2123123], ["b", 3], ["c", 3]]), b: ex2, c: [...ex3(), "1", 1]}}}}}}}}}}]]}]}, {d: [[123, 1, "1111", 23323, 1222, 55, [121322332, 12132322, 12323212], {a: 111, b: null, c: () => 1123231, d: 22, e: "e", f: () => "fffqqq"}], [2212, 1, "111211", 23, 121, 22, [33, 3 , 3], {a: 3, b: null, c: () => 1123221, d: 211, e: "e1231", f: ()=> "fffqq1232312123q", g: 2123}], [11, 22, "dsds", {a: {b: {c: {d: {a: 1, b: [{a: 13}], c: {a: {b: {c: {d: {a: new Map ([[true, 222312], [2, 322], ["c2", 32], [() => {}, 32]]), b: ex2, c: [...ex3 (), "1121123", 1]}}}}}}}}}}]]}, {a: {b: {c: {d: {a: 112312, b: [1], c: {a: {b: {c: {d: {a: "", b: "", c: "", v: "v12312323"}}}}}}}}}}]

console .log (sumNumbers (hardStructure))

我们有四个基本案例:

  • 对于零值(nullundefined),我们 return 0
  • Number 上,我们 return 它的值
  • String 上,我们调用 parseInt 和 return 值。这是我认为您最有可能需要更改的部分
  • 对于未知类型(可能是正则表达式或日期),我们 return 0

其他为递归情况:

  • 对于 Array,我们递归地调用每个元素上的函数,然后将它们加在一起。
  • 对于 Function,我们调用不带参数的函数,并在结果上重复。
  • 对于我们的其他已知类型(ObjectSetMap),我们将其值提取到 Array 中并重复结果。