как получить доступ в json объект - PullRequest
0 голосов
/ 20 апреля 2020

Я пытаюсь создать веб-страницу, которая использует APi от Breaking bad website, И с этого сайта я получил данные в формате JSON, и я много пробовал, но не понимаю, как я могу получить доступ только объекты, "автором" которых является "Уолтер Уайт", это полученные данные.

[{"quote_id": 1, "quote": "Я не в опасности, Скайлер. Я опасность!", "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," цитата ":" Я наблюдал за Джейн d ie. Я был там. И я наблюдал за ней d ie. Я наблюдал за ее передозировкой и задыхался до смерти. Я мог бы спасти ее. Но я этого не сделал. "," Автор ":" Уолтер Уайт "," series ":" Breaking Bad "}, {" quote_id ": 5," quote ":" Скажи мое имя. "," Author ":" Walter White "," series ":" Breaking Bad "}]

Ответы [ 2 ]

0 голосов
/ 20 апреля 2020

Вы можете использовать массив .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; }
0 голосов
/ 20 апреля 2020

Вы можете использовать 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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...