代码之家  ›  专栏  ›  技术社区  ›  Diego

获取对象中所有数组的二维数组

  •  0
  • Diego  · 技术社区  · 6 年前

    我试图将一个对象中的所有数组放在一个单独的二维数组中,但是函数的递归性质有问题。我可以得到所有的数组,但是按照我写的方式,我得到了我不想要的多层嵌套。

    下面的代码返回

    [
      ["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;
    }
    
    3 回复  |  直到 6 年前
        1
  •  2
  •   CertainPerformance    6 年前

    最简单的调整是 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;
    }
        2
  •  1
  •   brk    6 年前

    创建一个简单的递归函数并检查 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
  •  1
  •   mpm    6 年前

    使用递归函数和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))