gpt4 book ai didi

javascript - 寻找 Actor JS

转载 作者:行者123 更新时间:2023-12-01 02:42:57 26 4
gpt4 key购买 nike

我正在创建一个函数来查找我搜索的 Actor 制作的所有电影,这些电影位于 js 文件中,如下所示:

var filmlist = [ 
{"Title":"Killer's Kiss", "Actors":"Frank Silvera, Jamie Smith, Irene Kane, Jerry Jarrett"}
];

var a = document.getElementById("film");

function findActors() {
var x = document.getElementById("actor").value;
var y = document.getElementById("actorfilm");

a.innerHTML = "Movies with this actor: "
for (var i=0; i<filmlist.length; i++) {
if (x.indexOf(filmlist[i].Actors) !== -1) {
y.innerHTML = (filmlist[i].Title + "<br/>")
}
}
}
<textarea rows="2" cols="50" id="actor" placeholder="write actor name, and press the button"></textarea>
<button onclick="findActor()">ActorButton</button>

<p id="film"></p>
<p id="actorfilm"></p>

由于某种原因,它不会显示 Actor ,但它显示第一个 a.innerhtml我对 javascript 非常陌生,因此非常感谢您的帮助。

最佳答案

您已经快完成了,只是需要反转您的 if 条件。

if (filmlist[i].Actors.indexOf(x) )

在您输入的值内检查参与者的超集,而不是相反。

您可以通过这样做使其更加精确

var filteredFilms = filmlist.filter( s => s.Actors.indexOf(x) != -1 );
var output = filteredFilms.map( s => s.Title ).join( "<br/>" );

演示

var filmlist = [{
"Title": "Killer's Kiss",
"Actors": "Frank Silvera, Jamie Smith, Irene Kane, Jerry Jarrett"
}];

var a = document.getElementById("film");

function findActors() {

var x = document.getElementById("actor").value;
var y = document.getElementById("actorfilm");
a.innerHTML = "Movies with this actor: "
var filteredFilms = filmlist.filter(s => s.Actors.indexOf(x) != -1);
y.innerHTML = filteredFilms.map(s => s.Title).join("<br/>");

}
<!-- find actor function -->
<textarea rows="2" cols="50" id="actor" placeholder="write
actor name, and press the button">Frank Silvera</textarea>

<button onclick="findActors()">ActorButton</button>


<p id="film"></p>

<p id="actorfilm"></p>

关于javascript - 寻找 Actor JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47437640/

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