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.
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));
javascript function object recursion
add a comment |
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));
javascript function object recursion
You don't return anything from nestedrecursiveFunc
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 withfalse
as a parameter as well. There is no infinite recursion in that case.
– briosheje
Nov 22 '18 at 11:15
add a comment |
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));
javascript function object recursion
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
javascript function object recursion
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 nestedrecursiveFunc
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 withfalse
as a parameter as well. There is no infinite recursion in that case.
– briosheje
Nov 22 '18 at 11:15
add a comment |
You don't return anything from nestedrecursiveFunc
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 withfalse
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
add a comment |
3 Answers
3
active
oldest
votes
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));
Infinite recursion when there is adata = 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
add a comment |
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);
add a comment |
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);
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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));
Infinite recursion when there is adata = 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
add a comment |
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));
Infinite recursion when there is adata = 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
add a comment |
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));
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));
answered Nov 22 '18 at 10:49
osiris85osiris85
1466
1466
Infinite recursion when there is adata = 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
add a comment |
Infinite recursion when there is adata = 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
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
edited Nov 22 '18 at 11:01
answered Nov 22 '18 at 10:56
ZimZim
1,0741817
1,0741817
add a comment |
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered Nov 22 '18 at 11:04
MariusMarius
867
867
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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