【发布时间】:2019-03-23 14:53:05
【问题描述】:
这是一个逻辑问题。 我努力了,还是解决不了!
例如。
我有一个数组:["tech", "review", "howto"]。
我有另一个带有 id(s) 和文本的巨大数组。 ????
[
{ id: "tech", text: "Technology" },
{ id: "fin", text: "Finance" },
{ id: "digimark", text: "Digital Marketing" },
{ id: "coding", text: "Programming" },
{ id: "tutorial", text: "Tutorial" },
{ id: "howto", text: "How To" },
{ id: "writing", text: "Writing" },
{ id: "inspire", text: "Inspirational" },
{ id: "science", text: "Science" },
{ id: "politics", text: "Politics" },
{ id: "lifestyle", text: "Lifestyle" },
{ id: "food", text: "Food" },
{ id: "business", text: "Business" },
{ id: "entrepreneur", text: "Entrepreneurs" },
{ id: "history", text: "History" },
{ id: "health", text: "Health" },
{ id: "pet", text: "Pets" },
{ id: "parenthood", text: "Parenthood" },
{ id: "travel", text: "Travel" },
{ id: "india", text: "India" },
{ id: "china", text: "China" },
{ id: "uk", text: "United Kingdom" },
{ id: "us", text: "United States" },
{ id: "world", text: "World" },
{ id: "news", text: "News" },
{ id: "review", text: "Product Review" }
]
使用这些资产,我希望在 javascript 中得到这个响应:
[
{ id: "tech", text: "Technology" },
{ id: "review", text: "Product Review" },
{ id: "howto", text: "How To" }
]
就目前而言,我正在这样做????
categorySorter(categories) {
const categoryState = categorySuggessions.filter(category => category.id === categories.map(category => (category)))
return categoryState
}
这会返回一个空白(即[])数组。
你有什么建议?
NEWBIE HERE。
【问题讨论】:
标签: javascript arrays json for-loop ecmascript-6