[英]How to filter Array in an object
我的 object 中有這些 arrays,我需要使用特定值filter
tags
數組。 我不確定如何實現這一目標
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)
使用filter
並includes
一個優雅的單行
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);
我很確定你想過濾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)
希望這可以幫助..
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"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.