I have two array, the first array is an articles array which is composed by attributes and the second array is composed by element articles_id of the first array and I would like to merge the first array with the second array in new_array_articles_sub_categories if articles.id == subcategories.article_id
articles = [
{
id: 1,
title: xxx,
description: desc1
},
{
id: 2,
title: yyy,
description: desc2
}
]
subcategories = [
{
id: 1,
name: xxx,
article_id: 1
},
{
id: 2,
name: yyy,
article_id: 1
}
]
new_array_articles_sub_categories = [
{
id: 1,
title: xxx,
description: desc1
subcategories: [
[0] : [
id: 1,
name: xxx,
]
[1] : [
id: 2,
name: yyy,
]
]
},
{
id: 2,
title: yyy,
description: desc2,
subcategories: []
}
]
I try with this, but in output it is not the same result
var new_array_articles_sub_categories = []
for (var i = 0; i < articles.length; i++) {
for (var k = 0; k < subcategories.length; k++) {
if(articles[i].id == subcategories[k].article_id){
new_array_articles_sub_categories.push({
articles : articles[i],
subcategories : subcategories[k],
})
}
}
}