简体   繁体   English

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

[英]Return an array of objects from a recursive function in Javascript

I'm working on recursive functions. 我正在研究递归函数。

I must push all objects that have the key "data: true" in an array. 我必须在数组中推送所有具有键“ data:true”的对象。 The console.log in the middle of my function gives me all those objects in separate arrays. 我函数中间的console.log将所有这些对象放在单独的数组中。

But I can't return an array with the objects at the end. 但是我无法返回最后带有对象的数组。 What am I doing wrong? 我究竟做错了什么? Thanks 谢谢

 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: "ab", text: "abtext" }, c: { d: { data: true, key: "acd", text: "some.acd" } } } }; 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)); 

Add tab.concat() on the recursive call for join the items returned by the recursive fn. 在递归调用上添加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: "ab", text: "abtext" }, c: { d: { data: true, key: "acd", text: "some.acd" } } } }; 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)); 

You can pass an array as second argument that will act as an accumulator. 您可以将数组作为第二个参数传递,它将充当累加器。

Plus, I fixed your function that loops infinitely when data = false : 另外,我修复了当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)
      }

    }
  }
}

Usage: 用法:

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

You could use a global variable. 您可以使用全局变量。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM