【问题标题】:How to find the Array elements containing these IDs? [duplicate]如何找到包含这些 ID 的 Array 元素? [复制]
【发布时间】: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


    【解决方案1】:

    您可以使用filterincludes

    let arr = [{ 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" }]
    
    let ids = ["tech", "review", "howto"]
    
    let op = arr.filter(({id}) => ids.includes(id))
    
    console.log(op)

    【讨论】:

      【解决方案2】:

      您已经很接近了,只需使用.indexOf 来检查您的集合中是否存在密钥?

      const data = [
        { 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" }
      ];
      
      const set = ["tech", "review", "howto"];
      
      const result = data.filter(x => set.indexOf(x.id) !== -1);
      
      console.log(result);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-27
        • 1970-01-01
        • 2013-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多