微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有人可以帮我用所有相同的键从JSON制作数组吗?

如何解决有人可以帮我用所有相同的键从JSON制作数组吗?

这是我拥有的JSON

    [
    {
        "id": 1,"picture": "image-hero-paramour.jpg","title": "Project Paramour","subheading": "Project made for an art museum near Southwest London. Project Paramour is a statement of bold,modern architecture."
    },{
        "id": 2,"picture": "image-hero-seraph.jpg","title": "Seraph Station","subheading": "The Seraph Station project challenged us to design a unique station that would transport people through time. The result is a fresh and futuristic model inspired by space stations."
    },{
        "id": 3,"picture": "image-hero-federal.jpg","title": "Federal II Tower","subheading": "A sequel theme project for a tower originally built in the 1800s. We achieved this with a striking look of brutal minimalism with modern touches."
    },{
        "id": 4,"picture": "image-hero-trinity.jpg","title": "Trinity Bank Tower","subheading": "Trinity Bank challenged us to make a concept for a 84 story building located in the middle of a city with a high earthquake frequency. For this project we used curves to blend design and stability to meet our objectives."
    }
]

如果我想制作一个像这样的数组: let titles = ["Project Paramour","Seraph Station","Federal II Tower","Trinity Bank Tower"], 我该怎么办? 并为其他3个JSON密钥另外创建3个数组。

谢谢!

解决方法

您可以使用map()

console.log(data.map(i => i.title))

,

您可以使用map函数并选择要存储在新数组中的属性,像这样

const ids = array.map(object => object.id)
const titles = array.map(object => object.title)
const pictures = array.map(object => object.picture)
const subheadings = array.map(object => object.subheading)
,

您可以使用one函数从每个对象中选择一个特定字段。

例如在此示例中,titles是所有标题的列表,而pictures是所有图片的列表:

const array = [{
    "id": 1,"picture": "image-hero-paramour.jpg","title": "Project Paramour","subheading": "Project made for an art museum near Southwest London. Project Paramour is a statement of bold,modern architecture."
  },{
    "id": 2,"picture": "image-hero-seraph.jpg","title": "Seraph Station","subheading": "The Seraph Station project challenged us to design a unique station that would transport people through time. The result is a fresh and futuristic model inspired by space stations."
  },{
    "id": 3,"picture": "image-hero-federal.jpg","title": "Federal II Tower","subheading": "A sequel theme project for a tower originally built in the 1800s. We achieved this with a striking look of brutal minimalism with modern touches."
  },{
    "id": 4,"picture": "image-hero-trinity.jpg","title": "Trinity Bank Tower","subheading": "Trinity Bank challenged us to make a concept for a 84 story building located in the middle of a city with a high earthquake frequency. For this project we used curves to blend design and stability to meet our objectives."
  }
]

const titles = array.map(item => item.title)
const pictures = array.map(item => item.picture)

console.log({
  titles,pictures
})

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。