Return an array of objects from a recursive function in Javascript












0















I'm working on recursive functions.



I must push all objects that have the key "data: true" in an array.
The console.log in the middle of my function gives me all those objects in separate arrays.



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












share|improve this question

























  • You don't return anything from nested recursiveFunc call.

    – hindmost
    Nov 22 '18 at 10:47








  • 2





    You are never using the result of the recursive calls. Just assign it to something and push it to the original array (or concat it, or whatever): jsfiddle.net/aco2qryn

    – briosheje
    Nov 22 '18 at 10:49











  • Thanks a lot briosheje, it works now. I had tried something like this but forgot the spread operator.

    – Charlote22
    Nov 22 '18 at 10:55











  • None of the answers corrected the infinite recursion when data is false.

    – Zim
    Nov 22 '18 at 11:00











  • @Zim that's not true, my fiddle above works with false as a parameter as well. There is no infinite recursion in that case.

    – briosheje
    Nov 22 '18 at 11:15
















0















I'm working on recursive functions.



I must push all objects that have the key "data: true" in an array.
The console.log in the middle of my function gives me all those objects in separate arrays.



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












share|improve this question

























  • You don't return anything from nested recursiveFunc call.

    – hindmost
    Nov 22 '18 at 10:47








  • 2





    You are never using the result of the recursive calls. Just assign it to something and push it to the original array (or concat it, or whatever): jsfiddle.net/aco2qryn

    – briosheje
    Nov 22 '18 at 10:49











  • Thanks a lot briosheje, it works now. I had tried something like this but forgot the spread operator.

    – Charlote22
    Nov 22 '18 at 10:55











  • None of the answers corrected the infinite recursion when data is false.

    – Zim
    Nov 22 '18 at 11:00











  • @Zim that's not true, my fiddle above works with false as a parameter as well. There is no infinite recursion in that case.

    – briosheje
    Nov 22 '18 at 11:15














0












0








0








I'm working on recursive functions.



I must push all objects that have the key "data: true" in an array.
The console.log in the middle of my function gives me all those objects in separate arrays.



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












share|improve this question
















I'm working on recursive functions.



I must push all objects that have the key "data: true" in an array.
The console.log in the middle of my function gives me all those objects in separate arrays.



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








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





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






javascript function object recursion






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 10:47









ksav

5,35821331




5,35821331










asked Nov 22 '18 at 10:44









Charlote22Charlote22

255




255













  • You don't return anything from nested recursiveFunc call.

    – hindmost
    Nov 22 '18 at 10:47








  • 2





    You are never using the result of the recursive calls. Just assign it to something and push it to the original array (or concat it, or whatever): jsfiddle.net/aco2qryn

    – briosheje
    Nov 22 '18 at 10:49











  • Thanks a lot briosheje, it works now. I had tried something like this but forgot the spread operator.

    – Charlote22
    Nov 22 '18 at 10:55











  • None of the answers corrected the infinite recursion when data is false.

    – Zim
    Nov 22 '18 at 11:00











  • @Zim that's not true, my fiddle above works with false as a parameter as well. There is no infinite recursion in that case.

    – briosheje
    Nov 22 '18 at 11:15



















  • You don't return anything from nested recursiveFunc call.

    – hindmost
    Nov 22 '18 at 10:47








  • 2





    You are never using the result of the recursive calls. Just assign it to something and push it to the original array (or concat it, or whatever): jsfiddle.net/aco2qryn

    – briosheje
    Nov 22 '18 at 10:49











  • Thanks a lot briosheje, it works now. I had tried something like this but forgot the spread operator.

    – Charlote22
    Nov 22 '18 at 10:55











  • None of the answers corrected the infinite recursion when data is false.

    – Zim
    Nov 22 '18 at 11:00











  • @Zim that's not true, my fiddle above works with false as a parameter as well. There is no infinite recursion in that case.

    – briosheje
    Nov 22 '18 at 11:15

















You don't return anything from nested recursiveFunc call.

– hindmost
Nov 22 '18 at 10:47







You don't return anything from nested recursiveFunc call.

– hindmost
Nov 22 '18 at 10:47






2




2





You are never using the result of the recursive calls. Just assign it to something and push it to the original array (or concat it, or whatever): jsfiddle.net/aco2qryn

– briosheje
Nov 22 '18 at 10:49





You are never using the result of the recursive calls. Just assign it to something and push it to the original array (or concat it, or whatever): jsfiddle.net/aco2qryn

– briosheje
Nov 22 '18 at 10:49













Thanks a lot briosheje, it works now. I had tried something like this but forgot the spread operator.

– Charlote22
Nov 22 '18 at 10:55





Thanks a lot briosheje, it works now. I had tried something like this but forgot the spread operator.

– Charlote22
Nov 22 '18 at 10:55













None of the answers corrected the infinite recursion when data is false.

– Zim
Nov 22 '18 at 11:00





None of the answers corrected the infinite recursion when data is false.

– Zim
Nov 22 '18 at 11:00













@Zim that's not true, my fiddle above works with false as a parameter as well. There is no infinite recursion in that case.

– briosheje
Nov 22 '18 at 11:15





@Zim that's not true, my fiddle above works with false as a parameter as well. There is no infinite recursion in that case.

– briosheje
Nov 22 '18 at 11:15












3 Answers
3






active

oldest

votes


















1














Add tab.concat() on the recursive call for join the items returned by the recursive 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));








share|improve this answer
























  • Infinite recursion when there is a data = false. See corrected answer: stackoverflow.com/a/53429407/2898738

    – Zim
    Nov 22 '18 at 10:58











  • Not is infinite, u can try my code...

    – osiris85
    Nov 22 '18 at 11:01



















0














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:



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





share|improve this answer

































    0














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





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429174%2freturn-an-array-of-objects-from-a-recursive-function-in-javascript%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Add tab.concat() on the recursive call for join the items returned by the recursive 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));








      share|improve this answer
























      • Infinite recursion when there is a data = false. See corrected answer: stackoverflow.com/a/53429407/2898738

        – Zim
        Nov 22 '18 at 10:58











      • Not is infinite, u can try my code...

        – osiris85
        Nov 22 '18 at 11:01
















      1














      Add tab.concat() on the recursive call for join the items returned by the recursive 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));








      share|improve this answer
























      • Infinite recursion when there is a data = false. See corrected answer: stackoverflow.com/a/53429407/2898738

        – Zim
        Nov 22 '18 at 10:58











      • Not is infinite, u can try my code...

        – osiris85
        Nov 22 '18 at 11:01














      1












      1








      1







      Add tab.concat() on the recursive call for join the items returned by the recursive 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));








      share|improve this answer













      Add tab.concat() on the recursive call for join the items returned by the recursive 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));








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





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






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 22 '18 at 10:49









      osiris85osiris85

      1466




      1466













      • Infinite recursion when there is a data = false. See corrected answer: stackoverflow.com/a/53429407/2898738

        – Zim
        Nov 22 '18 at 10:58











      • Not is infinite, u can try my code...

        – osiris85
        Nov 22 '18 at 11:01



















      • Infinite recursion when there is a data = false. See corrected answer: stackoverflow.com/a/53429407/2898738

        – Zim
        Nov 22 '18 at 10:58











      • Not is infinite, u can try my code...

        – osiris85
        Nov 22 '18 at 11:01

















      Infinite recursion when there is a data = false. See corrected answer: stackoverflow.com/a/53429407/2898738

      – Zim
      Nov 22 '18 at 10:58





      Infinite recursion when there is a data = false. See corrected answer: stackoverflow.com/a/53429407/2898738

      – Zim
      Nov 22 '18 at 10:58













      Not is infinite, u can try my code...

      – osiris85
      Nov 22 '18 at 11:01





      Not is infinite, u can try my code...

      – osiris85
      Nov 22 '18 at 11:01













      0














      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:



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





      share|improve this answer






























        0














        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:



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





        share|improve this answer




























          0












          0








          0







          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:



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





          share|improve this answer















          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:



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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 11:01

























          answered Nov 22 '18 at 10:56









          ZimZim

          1,0741817




          1,0741817























              0














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





              share|improve this answer




























                0














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





                share|improve this answer


























                  0












                  0








                  0







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





                  share|improve this answer













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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 11:04









                  MariusMarius

                  867




                  867






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53429174%2freturn-an-array-of-objects-from-a-recursive-function-in-javascript%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      Origin of the phrase “under your belt”?