I am working with two sets of data
var JSON_Categories = '[{ "id" : "1", "text" : "Category A"}, { "id" : 2, "text" : "Category B" }]';
var JSON_Article = '[{ "id" : "1", "text" : "Article text A"}, { "id" : 3, "text" : "Article B"}]';
var categories = JSON.parse(JSON_Categories);
var article = JSON.parse(JSON_Article);
In the scenario where the id matches in both JSON objects, I need to pair the category text value with the corresponding article text value.
I attempted a solution but I know it's not ideal. Can anyone provide assistance?
var categoryValue = new Object();
categories.forEach(function(category) {
articles.forEach(article => {
if (category.id === article.id) {
categoryValue.id = category.id;
categoryValue.text = article.text;
} else {
categoryValue.id = category.id;
categoryValue.text = category.text;
}
});
});
console.log('categoryValue', categoryValue);