【问题标题】:Get 2-dimensional array of all arrays within an object获取对象内所有数组的二维数组
【发布时间】:2018-12-30 11:46:44
【问题描述】:

我正在尝试将对象中的所有数组放入单独的二维数组中,但在函数的递归性质方面遇到了问题。我能够得到所有的数组,但是我写它的方式,我得到了我不想要的多层嵌套。

下面的代码返回

[
  ["1-1","1-2","1-3"],
  [
    ["2-1","2-2","2-3"]
  ],
  [
    [
      ["3-1","3-2","3-3"]
    ]
  ],
  [
    [
      ["4-1-1","4-1-2","4-1-3"],
      [
        ["4-2-1-1","4-2-1-2","4-2-1-3"],
        ["4-2-2-1","4-2-2-2","4-2-2-3"]
      ]
    ]
  ]
]

如何修改getArrays() 函数以返回二维数组,而不考虑对象内的嵌套?

function testGetArrays() {
  var testObject = {
    "one": ["1-1", "1-2", "1-3"],
    "two": {
      "first": ["2-1", "2-2", "2-3"]
    }, 
    "three": {
      "first": {
        "second": ["3-1", "3-2", "3-3"]
      }
    }, 
    "four": {
      "first": {
        "first": ["4-1-1", "4-1-2", "4-1-3"],
        "second": {
          "first": ["4-2-1-1", "4-2-1-2", "4-2-1-3"],
          "second": ["4-2-2-1", "4-2-2-2", "4-2-2-3"]
        }
      }
    }
  };
  var expectedResult = [
    ["1-1", "1-2", "1-3"],
    ["2-1", "2-2", "2-3"],
    ["3-1", "3-2", "3-3"],
    ["4-1-1", "4-1-2", "4-1-3"],
    ["4-2-1-1", "4-2-1-2", "4-2-1-3"],
    ["4-2-2-1", "4-2-2-2", "4-2-2-3"]
  ];
  var result = getArrays(testObject);
  console.log(JSON.stringify(expectedResult) == JSON.stringify(result));
  console.log(JSON.stringify(result));
}

function getArrays(object) {
  var result = [];
  if (Array.isArray(object)) {
    if (Array.isArray(object[0])) {
      result.push(getArrays(object[0]));
    }
    result.push(object);
  } else {
    for (var i in object) {
      current = object[i];
      if (Array.isArray(current)) {
        result.push(current);
      } else {
        var x = getArrays(current);
        result.push(x);
      }
    }
  }
  return result;
}

【问题讨论】:

  • result 传入getArrays(或在外部定义),而不是每次调用时都创建一个新的结果数组

标签: javascript arrays object recursion multidimensional-array


【解决方案1】:

使用递归函数和 ES2015:

const collectArrays = function(object){
  if (Array.isArray(object)) {
    return [object];
  }
  return Object.keys(object).reduce(function(result, key){
    result.push(...collectArrays(object[key]))
    return result;
  }, []);
}

var testObject = {
  "one": ["1-1", "1-2", "1-3"],
  "two": {
    "first": ["2-1", "2-2", "2-3"]
  },
  "three": {
    "first": {
      "second": ["3-1", "3-2", "3-3"]
    }
  },
  "four": {
    "first": {
      "first": ["4-1-1", "4-1-2", "4-1-3"],
      "second": {
        "first": ["4-2-1-1", "4-2-1-2", "4-2-1-3"],
        "second": ["4-2-2-1", "4-2-2-2", "4-2-2-3"]
      }
    }
  }
};

console.log(collectArrays(testObject))

【讨论】:

    【解决方案2】:

    创建一个简单的递归函数并检查键的value。如果它是一个数组,则推送到一个新数组,如果它是一个对象,则调用相同的递归函数

    var testObject = {
      "one": ["1-1", "1-2", "1-3"],
      "two": {
        "first": ["2-1", "2-2", "2-3"]
      },
      "three": {
        "first": {
          "second": ["3-1", "3-2", "3-3"]
        }
      },
      "four": {
        "first": {
          "first": ["4-1-1", "4-1-2", "4-1-3"],
          "second": {
            "first": ["4-2-1-1", "4-2-1-2", "4-2-1-3"],
            "second": ["4-2-2-1", "4-2-2-2", "4-2-2-3"]
          }
        }
      }
    };
    let newArr = [];
    
    function getKeys(obj) {
      //iterate over the object
      for (let keys in obj) {
        //check if the value is an array
        if (Array.isArray(obj[keys])) {
          // if so then push the value to another array
          newArr.push(obj[keys])
          // if not an array the recall the recursive function
        } else if (typeof obj[keys] === 'object') {
          getKeys(obj[keys])
        }
      }
    }
    getKeys(testObject);
    console.log(newArr)

    【讨论】:

      【解决方案3】:

      最简单的调整是给getArrays 另一个参数result,它在第一次调用时被初始化为一个空数组,然后递归地传递和变异:

      testGetArrays();
      function testGetArrays() {
        var testObject = {
          "one": ["1-1", "1-2", "1-3"],
          "two": {
            "first": ["2-1", "2-2", "2-3"]
          }, 
          "three": {
            "first": {
              "second": ["3-1", "3-2", "3-3"]
            }
          }, 
          "four": {
            "first": {
              "first": ["4-1-1", "4-1-2", "4-1-3"],
              "second": {
                "first": ["4-2-1-1", "4-2-1-2", "4-2-1-3"],
                "second": ["4-2-2-1", "4-2-2-2", "4-2-2-3"]
              }
            }
          }
        };
        var expectedResult = [
          ["1-1", "1-2", "1-3"],
          ["2-1", "2-2", "2-3"],
          ["3-1", "3-2", "3-3"],
          ["4-1-1", "4-1-2", "4-1-3"],
          ["4-2-1-1", "4-2-1-2", "4-2-1-3"],
          ["4-2-2-1", "4-2-2-2", "4-2-2-3"]
        ];
        var result = getArrays(testObject);
        console.log(JSON.stringify(expectedResult) == JSON.stringify(result));
        console.log(JSON.stringify(result));
        console.log(result);
      }
      
      function getArrays(object, result = []) {
        if (Array.isArray(object)) {
          if (Array.isArray(object[0])) {
            result.push(getArrays(object[0]));
          }
          result.push(object);
        } else {
          for (var i in object) {
            current = object[i];
            if (Array.isArray(current)) {
              result.push(current);
            } else {
              getArrays(current, result);
            }
          }
        }
        return result;
      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-24
        • 1970-01-01
        • 2015-01-07
        相关资源
        最近更新 更多