javascript 如何过滤对象中的数组

htrmnn0y  于 2023-01-16  发布在  Java
关注(0)|答案(3)|浏览(188)

我的对象中有这些数组,我需要filtertags数组的特定值。我不知道如何实现这一点

const obj = [
    {
        "slug": "add-an-aggregate-rating-feature-to-your-website",
        "frontmatter": {
            "title": "Add An Aggregate Rating Feature To Your Website",
            "metaTitle": "Add An Aggregate Rating Feature To Your Website",
            "tags": [
                "structured-data",
                "aggregate-rating",
                "rich-text"
            ]
        }
    },
    {
        "slug": "step-by-step-guide-to-become-a-full-stack-web-developer-in-2023",
        "frontmatter": {
            "title": "Step-by-Step Guide: How to Become a Full Stack Web Developer in 2023",
            "metaTitle": "Step-by-Step Guide: How to Become a Full Stack Web Developer"
            "tags": [
                "article",
                "roadmap"
            ]
        }
    },
    {
        "slug": "what-is-dall-e",
        "frontmatter": {
            "title": "What is DALL-E? A world changing technology?",
            "metaTitle": "What is DALL-E? A world changing technology?"
            "tags": [
                "technology",
                "article",
                "openai"
            ]
        }
    }
]

// List objects having article tag only
var newArray = obj.filter(function (el){
    return el.frontmatter.tags.filter(function (el2){
         el2 == "article"
    })
})
console.log(newArray)
bfrts1fy

bfrts1fy1#

我非常确定您要过滤obj数组

const newArray = obj.filter((el) =>
  el.frontmatter.tags.includes("article")
)
const obj = [{
    "slug": "add-an-aggregate-rating-feature-to-your-website",
    "frontmatter": {
      "title": "Add An Aggregate Rating Feature To Your Website",
      "metaTitle": "Add An Aggregate Rating Feature To Your Website",
      "tags": [
        "structured-data",
        "aggregate-rating",
        "rich-text"
      ]
    }
  },
  {
    "slug": "step-by-step-guide-to-become-a-full-stack-web-developer-in-2023",
    "frontmatter": {
      "title": "Step-by-Step Guide: How to Become a Full Stack Web Developer in 2023",
      "metaTitle": "Step-by-Step Guide: How to Become a Full Stack Web Developer",
      "tags": [
        "article",
        "roadmap"
      ]
    }
  },
  {
    "slug": "what-is-dall-e",
    "frontmatter": {
      "title": "What is DALL-E? A world changing technology?",
      "metaTitle": "What is DALL-E? A world changing technology?",
      "tags": [
        "technology",
        "article",
        "openai"
      ]
    }
  }
]

const newArray = obj.filter((el) =>
  el.frontmatter.tags.includes("article")
)

console.log(newArray)
w8ntj3qf

w8ntj3qf2#

使用filterincludes创建优雅的一行程序

const obj = [
    {
        "slug": "add-an-aggregate-rating-feature-to-your-website",
        "frontmatter": {
            "title": "Add An Aggregate Rating Feature To Your Website",
            "metaTitle": "Add An Aggregate Rating Feature To Your Website",
            "tags": [
                "structured-data",
                "aggregate-rating",
                "rich-text"
            ]
        }
    },
    {
        "slug": "step-by-step-guide-to-become-a-full-stack-web-developer-in-2023",
        "frontmatter": {
            "title": "Step-by-Step Guide: How to Become a Full Stack Web Developer in 2023",
            "metaTitle": "Step-by-Step Guide: How to Become a Full Stack Web Developer",
            "tags": [
                "article",
                "roadmap"
            ]
        }
    },
    {
        "slug": "what-is-dall-e",
        "frontmatter": {
            "title": "What is DALL-E? A world changing technology?",
            "metaTitle": "What is DALL-E? A world changing technology?",
            "tags": [
                "technology",
                "article",
                "openai"
            ]
        }
    }
];

const newArray = obj.filter(el => el.frontmatter.tags.includes("article"));
console.log(newArray);
e5nqia27

e5nqia273#

希望这对你有帮助。

const blogs = [
  {
    "slug": "add-an-aggregate-rating-feature-to-your-website",
    "frontmatter": {
      "title": "Add An Aggregate Rating Feature To Your Website",
      "metaTitle": "Add An Aggregate Rating Feature To Your Website",
      "tags": [
        "structured-data",
        "aggregate-rating",
        "rich-text"
      ]
    }
  },
  {
    "slug": "step-by-step-guide-to-become-a-full-stack-web-developer-in-2023",
    "frontmatter": {
      "title": "Step-by-Step Guide: How to Become a Full Stack Web Developer in 2023",
      "metaTitle": "Step-by-Step Guide: How to Become a Full Stack Web Developer",
      "tags": [
        "article",
        "roadmap"
      ]
    }
  },
  {
    "slug": "what-is-dall-e",
    "frontmatter": {
      "title": "What is DALL-E? A world changing technology?",
      "metaTitle": "What is DALL-E? A world changing technology?",
      "tags": [
        "technology",
        "article",
        "openai"
      ]
    }
  }
]

// List objects having article tag only
const blogsWithTag = blogs.filter((blog) => 
    blog.frontmatter?.tags.includes("article"))

相关问题