为了账号安全,请及时绑定邮箱和手机立即绑定

如何访问 json 对象

如何访问 json 对象

慕娘9325324 2022-07-08 18:20:22
我正在尝试创建网页,它使用来自 Breaking bad website 的 APi,并且从这个网站我收到了 JSON 格式的数据,我尝试了很多,但不明白,我如何才能只访问“作者”的对象" 是 "Walter White" 这是接收到的数据。[{"quote_id":1,"quote":"我没有危险,Skyler。我就是危险!","author":"Walter White","series":"Breaking Bad"},{"quote_id ":2,"quote":"远离我的领域。","author":"Walter White","series":"Breaking Bad"},{"quote_id":3,"quote":"IFT" ,"author":"Skyler White","series":"Breaking Bad"},{"quote_id":4,"quote":"我看着简死去。我在那儿。我看着她死去。我看着她过量并窒息而死。我本可以救她。但我没有。","author":"Walter White","series":"Breaking Bad"},{"quote_id":5,"quote": “说我的名字。”,”author":"Walter White","series":"Breaking Bad"}]
查看完整描述

2 回答

?
Qyouu

TA贡献1786条经验 获得超11个赞

您可以在此处使用数组.filter()方法,例如:


var data = [{quote_id:1,quote:"I am not in danger, Skyler. I am the danger!",author:"Walter White",series:"Breaking Bad"},{quote_id:2,quote:"Stay out of my territory.",author:"Walter White",series:"Breaking Bad"},{quote_id:3,quote:"IFT",author:"Skyler White",series:"Breaking Bad"},{quote_id:4,quote:"I watched Jane die. I was there. And I watched her die. I watched her overdose and choke to death. I could have saved her. But I didn’t.",author:"Walter White",series:"Breaking Bad"},{quote_id:5,quote:"Say my name.",author:"Walter White",series:"Breaking Bad"}];


var res = data.filter(d => d.author === 'Walter White')

console.log( res )

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反对 回复 2022-07-08
?
郎朗坤

TA贡献1921条经验 获得超9个赞

您可以使用filter. 注意toLowerCase()用于不区分大小写的结果。


const filterKey = 'walter white';

let data = [{

  "quote_id": 1,

  "quote": "I am not in danger, Skyler. I am the danger!",

  "author": "Walter White",

  "series": "Breaking Bad"

}, {

  "quote_id": 2,

  "quote": "Stay out of my territory.",

  "author": "Walter White",

  "series": "Breaking Bad"

}, {

  "quote_id": 3,

  "quote": "IFT",

  "author": "Skyler White",

  "series": "Breaking Bad"

}, {

  "quote_id": 4,

  "quote": "I watched Jane die. I was there. And I watched her die. I watched her overdose and choke to death. I could have saved her. But I didn’t.",

  "author": "Walter White",

  "series": "Breaking Bad"

}, {

  "quote_id": 5,

  "quote": "Say my name.",

  "author": "Walter White",

  "series": "Breaking Bad"

}].filter(item => item.author.trim().toLowerCase() === filterKey);


console.log(data)


查看完整回答
反对 回复 2022-07-08
  • 2 回答
  • 0 关注
  • 145 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信