Can anyone help me figure out how to insert the following object into an array called "posts"?
{ post_id: 1, text: "text", creation: "date" }
I have a complex array structure like this:
var posts = [
{
post_id: 5,
text: "text",
creation: "date"
},
{
group: "favPosts",
posts: [
{ post_id: 2, text: "text", creation: "date" },
{ post_id: 7, text: "text", creation: "date" }
]
},
{
post_id: 8,
text: "text",
creation: "date"
}
]
I've tried various methods like slice and push but couldn't quite get it working. I'm still learning JavaScript and struggling with this task.
While I did come up with a solution using a function called addToGroup, I believe it's not very efficient:
addToGroup("favPosts");
function addToGroup(group) {
for(id in posts) {
if(posts[id].group == group){
posts[id].posts.push({ post_id: 10, text: "did it", creation: "date" });
console.log(posts[id]);
}
}
}