gpt4 book ai didi

javascript - 如何使用 native javascript 将数据附加到列表中的数组中

转载 作者:行者123 更新时间:2023-11-29 20:40:01 25 4
gpt4 key购买 nike

我正在从 API 调用数据并按名称列出数据。我想从列表中按名称搜索和添加对象(使用“添加最喜欢的电影”按钮)并使用 native javascript 将该对象的全部数据存储在数组中。

我可以在 Angularjs 中做到这一点,但不太了解使用原生 javascript。

function addListItem(title, listId) {
var ul = document.getElementById(listId);
var li = document.createElement("li");
li.className = 'list-group-item';
li.appendChild(document.createTextNode(title));
ul.appendChild(li);
}

function afterLoad() {
var data = JSON.parse(this.responseText);
var name = document.createElement('img');

const results = data.results;

// loop through items
results.forEach(item => {
addListItem(item.title, "items");
});

name.src = data.title;
document.body.appendChild(name);
}

function afterClick() {
// changed target to focus search
var terms = document.getElementById("search").value.split(' ').join('+');
var request = new XMLHttpRequest();
request.addEventListener('load', afterLoad);
request.open('GET', 'https://api.themoviedb.org/3/search/movie?api_key=8318c431b4fc8a2c4762bf2a52c351ee&query='+terms);
request.send();

}

button.addEventListener("click", afterClick);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css"/>
<title>Get Movies</title>
</head>
<body>
<header id="main-header" class="bg-success text-white p-4 mb-3">
<div class="container">
<h1 id="header-title">Get Movies<span style="display:none">123</span></h1>
<input style="align:right" type="text" class="form-control mr-2" id="search">
<input type="submit" class="btn btn-dark" value="Search" id="button">
</div>
</header>
<div class="container">
<div id="main" class="card card-body">
<h2 class="title">Add Fav Movies</h2>
<form class="form-inline mb-3">
<input type="text" class="form-control mr-2">
<input type="submit" class="btn btn-dark" value="Submit">
</form>
<h2 class="title">Lists</h2>
<ul id="items" class="list-group">

</ul>
</div>
</div>
</body>
</html>

在响应 JSON 上,我想选择数组键 result 下的电影对象(我点击列表)。并将那个特定的电影对象附加到另一个数组上。我该怎么做?

最佳答案

当您在 input(最喜欢的电影)中键入电影名称并点击 submit 时,检查输入是否有任何值。如果有,则制作您最喜欢的电影列表的 textContent 数组,然后应用过滤器以获取名称与用户输入的值匹配的电影对象

function addListItem(title, listId) {
var ul = document.getElementById(listId);
var li = document.createElement("li");
li.className = 'list-group-item';
li.appendChild(document.createTextNode(title));
ul.appendChild(li);
}

let results;

function afterLoad() {
var data = JSON.parse(this.responseText);
var name = document.createElement('img');

results = data.results;

// loop through items
results.forEach(item => {
addListItem(item.title, "items");
});

name.src = data.title;
document.body.appendChild(name);
}

function afterClick() {
// changed target to focus search
var terms = document.getElementById("search").value.split(' ').join('+');
var request = new XMLHttpRequest();
request.addEventListener('load', afterLoad);
request.open('GET', 'https://api.themoviedb.org/3/search/movie?api_key=8318c431b4fc8a2c4762bf2a52c351ee&query='+terms);
request.send();
}

button.addEventListener("click", afterClick);

const submitBtn = document.querySelector('input[value="Submit"]');
const favMovie = document.querySelector('form > input');

submitBtn.addEventListener('click', function(e) {
e.preventDefault();
const favMovieName = favMovie.value;
if(favMovieName.length > 0) {
const filteredFavMovies = results.filter(({title}) => title.toLowerCase().includes(favMovieName.toLowerCase()));
console.log(filteredFavMovies);
}
});
<header id="main-header" class="bg-success text-white p-4 mb-3">
<div class="container">
<h1 id="header-title">Get Movies<span style="display:none">123</span></h1>
<input style="align:right" type="text" class="form-control mr-2" id="search">
<input type="submit" class="btn btn-dark" value="Search" id="button">
</div>
</header>
<div class="container">
<div id="main" class="card card-body">
<h2 class="title">Add Fav Movies</h2>
<form class="form-inline mb-3">
<input type="text" class="form-control mr-2">
<input type="submit" class="btn btn-dark" value="Submit">
</form>
<h2 class="title">Lists</h2>
<ul id="items" class="list-group">

</ul>
</div>
</div>

关于javascript - 如何使用 native javascript 将数据附加到列表中的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55823220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com