誰能告訴我解決這個js演算法?我正在到達嵌套物件物件以檢查與我的變數相同的類別值,如果為 true,則回傳這些物件與值匹配。不確定我是否使用了正確的方法,但也不確定如何回傳整個物件。
物件
const data = [
{
"id": 799,
"name": "Ship Your Idea",
"slug": "ship-your-idea-22",
"permalink": "https://example.com/product/ship-your-idea-22/",
"categories": [
{
"id": 9,
"name": "Clothing",
"slug": "clothing"
}
]
},
{
"id": 794,
"name": "Premium Quality",
"slug": "premium-quality-19",
"permalink": "https://example.com/product/premium-quality-19/",
"categories": [
{
"id": 14,
"name": "T-shirts",
"slug": "t-shirts"
}
]
},
{
"id": 795,
"name": "Premium Quality2",
"slug": "premium-quality-193",
"permalink": "https://example.com/product/premium-quality-193/",
"categories": [
{
"id": 15,
"name": "Clothing",
"slug": "clothing"
}
]
}
]
我的職能
const brand = 'clothing';
function fliterProductByCategorly(obj, brand){
// 1.loop through each objects object
// 2.loop through object and get categorie
// 3.categorie filter get slug
// 4.check if slug value are same value as global slug, if do only return object that match the brand value.
return Object.entries(obj).filter(([, {categories}]) => Object.entries(categories).filter(([, {slug}]) => slug == brand ))
}
console.log(fliterProductByCategorly(data, brand))
示例:https ://stackblitz.com/edit/js-pizeq4
uj5u.com熱心網友回復:
假設類別陣列只有 1 個元素
const data = [
{
"id": 799,
"name": "Ship Your Idea",
"slug": "ship-your-idea-22",
"permalink": "https://example.com/product/ship-your-idea-22/",
"categories": [
{
"id": 9,
"name": "Clothing",
"slug": "clothing"
}
]
},
{
"id": 794,
"name": "Premium Quality",
"slug": "premium-quality-19",
"permalink": "https://example.com/product/premium-quality-19/",
"categories": [
{
"id": 14,
"name": "T-shirts",
"slug": "t-shirts"
}
]
},
{
"id": 795,
"name": "Premium Quality2",
"slug": "premium-quality-193",
"permalink": "https://example.com/product/premium-quality-193/",
"categories": [
{
"id": 15,
"name": "Clothing",
"slug": "clothing"
}
]
}
]
const brand = 'clothing';
function fliterProductByCategorly(obj, brand){
return obj.filter(e => e.categories[0].slug===brand)
}
console.log(fliterProductByCategorly(data, brand))
.as-console-wrapper { max-height: 100% !important; top: 0; }
如果類別包含超過 1 個元素,則可能使用 reduce 實作。
const data = [
{
"id": 799,
"name": "Ship Your Idea",
"slug": "ship-your-idea-22",
"permalink": "https://example.com/product/ship-your-idea-22/",
"categories": [
{
"id": 9,
"name": "Clothing",
"slug": "clothing"
}
]
},
{
"id": 794,
"name": "Premium Quality",
"slug": "premium-quality-19",
"permalink": "https://example.com/product/premium-quality-19/",
"categories": [
{
"id": 14,
"name": "T-shirts",
"slug": "t-shirts"
}
]
},
{
"id": 795,
"name": "Premium Quality2",
"slug": "premium-quality-193",
"permalink": "https://example.com/product/premium-quality-193/",
"categories": [
{
"id": 16,
"name": "T-shirts",
"slug": "t-shirts"
},
{
"id": 15,
"name": "Clothing",
"slug": "clothing"
}
]
}
]
const brand = 'clothing';
function fliterProductByCategorly(obj, brand){
return obj.reduce((acc,curr) => {
if (curr.categories.filter(e => e.slug===brand).length>0){
acc.push(curr)
}
return acc;
},[])
}
console.log(fliterProductByCategorly(data, brand))
.as-console-wrapper { max-height: 100% !important; top: 0; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/417559.html
標籤:
上一篇:使陣列值的總和等于k
下一篇:迭代演算法變成遞回演算法
