如何在json对象中拥有acces



我正在尝试创建网页,该网页使用Breaking bad网站的APi,并且从该网站我收到了JSON格式的数据,我尝试了很多,但不明白,我如何才能拥有"author"是"Walter White"的仅访问对象这是接收到的数据。

[{"quote_id":1,"quote":"我没有危险,斯凯勒。我是危险的!","作者":"沃尔特·怀特","系列":"绝命毒师"},{"quote_id":2,"quot":"远离我的领地。"。","作者":"沃尔特·怀特","系列":"绝命毒师"},{"quote_id":3,"quote":"IFT","作家":"斯凯勒·怀特",系列":"绝命毒师大"},{"quote_id":4,作者":"Walter White","系列":"绝命毒师"}]

您可以在这里使用数组.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; }

您可以使用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)

最新更新