Надеюсь, это вам поможет:
var categorydata = [
{ category_id: 2, name: "Default Category", position: 1 },
{ category_id: 3, name: "Clothing", position: 14597 },
{ category_id: 6, name: "Brands", position: 1 },
{ category_id: 8, name: "What's New", position: 1011 },
{ category_id: 12, name: "Dresses", position: 4456 },
{ category_id: 128, name: "Current Mood", position: 189 },
{ category_id: 138, name: "Mini", position: 13078 },
{ category_id: 630, name: "Kandi - Rave Clothing", position: 264 },
{ category_id: 1130, name: "Char Test Category", position: 99 },
{ category_id: 2443, name: "Sets", position: 163 },
{ category_id: 2657, name: "Club Exx Festival", position: 208 }
];
var allcategorydatawithlevel = [
{ id: 1, level: 1 },
{ id: 2, level: 5 }
]
var category = [];
for (i = 0; i < allcategorydatawithlevel.length; i++) {
for (x = 0; x < categorydata.length; x++) {
if (allcategorydatawithlevel[i].id == categorydata[x].category_id) {
category.push(allcategorydatawithlevel[i].level);
}
}
}
localStorage.setItem("category", JSON.stringify(category));
console.log(JSON.stringify(category));
Вы также можете использовать код ниже:
allcategorydatawithlevel.forEach((item, index) => {
categorydata.forEach((key, val) => {
if (item.id === key.category_id) {
category.push(item.level);
}
})
localStorage.setItem("category", JSON.stringify(category));
})
console.log(JSON.stringify(category));