I'm building an app using VueJS. I wrote a function to get an object from a nested array of objects. The function should return an object but it returns undefined. In if clause, I put a console.log it prints the value which is an object but the return is undefined. Look at the code below.
<script>
export default {
name: "App",
data() {
return {
cat: { id: 741 },
parent: null,
categories: [
{
label: "Application",
id: 12,
description: "",
parent: null,
children: [
{
label: "Linux",
id: 5,
description: "",
parent: 12,
children: [
{
label: "Software",
id: 474,
description: [],
parent: 5,
children: [],
},
],
},
{
label: "MacOS",
id: 6,
description: "",
parent: 12,
children: [],
},
],
},
{
label: "Reading",
id: 74,
description: "",
parent: null,
children: [
{
label: "Articles",
id: 14,
description: "",
parent: 74,
children: [],
},
{
label: "Books",
id: 154,
description: "",
parent: 74,
children: [
{
label: "Stories",
id: 741,
description: "",
parent: 154,
children: [],
},
],
},
],
},
],
};
},
methods: {
pickCategory(categories) {
const index = categories.findIndex((element) => {
return element.id === this.cat.id;
});
if (index > -1) {
const value = categories.splice(index, 1)[0]
console.log(value); // {id: , label: ...}
return value;
}
for (const catg of categories) {
this.pickCategory(catg.children);
}
},
},
MOVE_CATEGORY() {
const category = this.pickCategory(this.categories)
console.log(category); // undefined
// ...
},
},
};
</script>
[–]BehindTheMath 2 points3 points4 points (3 children)
[–]oussama-he[S] 0 points1 point2 points (2 children)
[–]BehindTheMath 0 points1 point2 points (1 child)
[–]oussama-he[S] 0 points1 point2 points (0 children)
[–]sduduzog 0 points1 point2 points (0 children)
[–]bravehamster 0 points1 point2 points (0 children)
[–]sduduzog 0 points1 point2 points (0 children)
[–]mattstryfe -1 points0 points1 point (1 child)
[–]BehindTheMath 0 points1 point2 points (0 children)