gpt4 book ai didi

javascript - 我的函数不会返回要保存在数组中的对象

转载 作者:行者123 更新时间:2023-12-03 10:32:48 25 4
gpt4 key购买 nike

我无法获取返回对象并保存在数组中的代码,第一个函数是将歌曲保存在数组中,第二个函数是搜索已保存在数组中的歌曲

function save() {
//loop to keep adding songs within the array

var addSong = //create the object inside the function
{
title: document.number.title.value,
artist: document.number.artist.value,
col: document.number.col.value,
dur: document.number.dur.value
};

//output the song title of the saved song
document.number.saved.value = addSong.title;
return addSong;
}
//----------------------------------------------------------
var sTitle = document.searchSong.searchTitle.value; // assigning the search box to 'sTitle'
var library = new Array() //array to store each song
var song = addSong.title;

//-----------------------------------------------------------
//function to search for a song saved in the music() array
function search() {
//if statement to compare the inputs and see if the song is in the
library
if (sTitle == song) {
//if the song title is there then it is output
document.searchSong.result.value = song;
} else {
//if not then 'no song' is output instead
document.searchSong.result.value = "no song";
}
}

最佳答案

您需要调用函数 save() 并将其结果分配给名为 addSong 的变量。

var addSong = save();
var song = addSong.title;

关于javascript - 我的函数不会返回要保存在数组中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29133392/

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