suppose i have two arrays like below =>
const arr1 = {
books: ["a1","a2"],
categories: ["c1", "c2"]
}
const arr2 = [
{
"books": [{
label: "a1",
count: 2
},
{
label: "a3",
count: 4
}],
"categories": [{
label: "c2",
count: 2
},
{
label: "c3",
count: 4
}]
}]
How can i check if all the books in arr1 which is array of strings exist in arr2[0].books which is array of objects? If it doesnt exist then i want to create an object in arr2[0].books with {label: "whatever string doesn't exist in arr2", count: 0}.
I want to do it for each arr2 objects books and categories
I tried below code: -
arr1.books.map(i => {
return arr2[0].books.find(item => i == item.label);
});
Expected Result -
arr2 = [
{
"books": [{
label: "a1",
count: 2
},
{
label: "a3",
count: 4
}, {
label: "a2",
count: 0
}
],
"categories": [{
label: "c2",
count: 2
},
{
label: "c3",
count: 4
},{
label: "c1",
count: 0
}]
}]
which is label: a2 is added to books in arr2 as it didnt exist, and similarly c1 in arr2 of categories . and sort based on count desc
arr1an array? or object?