从JavaScript的递归函数返回对象数组

时间:2018-11-22 10:44:58

标签: javascript function object recursion

我正在研究递归函数。

我必须在数组中推送所有具有键“ data:true”的对象。 函数中间的console.log将所有这些对象放在单独的数组中。

但是我不能返回最后带有对象的数组。 我究竟做错了什么? 谢谢

const entries = {
  root: {
    data: true,
    key: "root",
    text: "some text"
  },
  test: {
    one: {
      two: {
        data: true,
        key: "test.one.two",
        text: "some text.again"
      },
      three: {
        data: true,
        key: "test.one.three",
        text: "some.more.text"
      }
    },
    other: {
      data: true,
      key: "test3",
      text: "sometext.text"
    }
  },
  a: {
    b: {
      data: true,
      key: "a.b",
      text: "a.b.text"
    },
    c: {
      d: {
        data: true,
        key: "a.c.d",
        text: "some.a.c.d"
      }
    }
  }
};


function recursiveFunc(data) {
  let tab = [];
  for (let property in data) {
    if (data.hasOwnProperty(property)) {
      if (data[property].data === true) {
        tab.push(data[property]);
        console.log("t", tab);
      } else {
        recursiveFunc(data[property])
      }
    }
  }
  return tab
}

console.log(recursiveFunc(entries));

3 个答案:

答案 0 :(得分:1)

在递归调用上添加tab.concat(),以加入递归fn返回的项。

const entries = {
  root: {
    data: true,
    key: "root",
    text: "some text"
  },
  test: {
    one: {
      two: {
        data: true,
        key: "test.one.two",
        text: "some text.again"
      },
      three: {
        data: true,
        key: "test.one.three",
        text: "some.more.text"
      }
    },
    other: {
      data: true,
      key: "test3",
      text: "sometext.text"
    }
  },
  a: {
    b: {
      data: true,
      key: "a.b",
      text: "a.b.text"
    },
    c: {
      d: {
        data: true,
        key: "a.c.d",
        text: "some.a.c.d"
      }
    }
  }
};


function recursiveFunc(data) {
  let tab = [];
  for (let property in data) {
    if (data.hasOwnProperty(property)) {
      if (data[property].data === true) {
        tab.push(data[property]);
        console.log("t", tab);
      } else { 
        tab = tab.concat(recursiveFunc(data[property]));
      }
    } 
  } 
  return tab
}
console.log(recursiveFunc(entries));

答案 1 :(得分:0)

您可以将数组作为第二个参数传递,它将作为累加器。

此外,我修复了在data = false时无限循环的函数:

function recursiveFunc(data, acc) {
  for (let property in data) {
    if (data.hasOwnProperty(property) && typeof data[property] === "object") {

      var current = data[property];

      if (current.data === true) {
        acc.push(current);
      } else {
        recursiveFunc(current, acc)
      }

    }
  }
}

用法:

var results = [];
recursiveFunc(entries, results);
console.log(results);

答案 2 :(得分:0)

您可以使用全局变量。

const entries = { ... };


var tab = [];

function getTab(data) {
    tab = [];
    recursiveFunc(data);
    return tab;
}
function recursiveFunc(data) {
  for (let property in data) {
    if (data.hasOwnProperty(property) && typeof data[property] === "object") {
      if (data[property].data === true) {
        tab.push(data[property]);
      } else {
        recursiveFunc(data[property])
      }
    }
  }
}

getTab(entries);