【发布时间】:2022-11-04 20:56:35
【问题描述】:
我正在尝试从表单中获取数据并将其添加到现有的对象movieData。我能够传递所有较低的键值对,但作为顶层键的标题无法识别并作为字符串传递。任何想法我怎么能做到这一点?在此先感谢您的帮助。
let movieData = {
"The Darjeeling Limited": {
plot: "A year after their father's funeral, three brothers travel across India by train in an attempt to bond with each other.",
cast: ["Jason Schwartzman", "Owen Wilson", "Adrien Brody"],
runtime: 151,
rating: 7.2,
year: 2007,
}
...
}
function handleForm(event) {
event.preventDefault();
const newTitle = document.getElementById("newTitle").value;
const newRating = document.getElementById("newRating").value;
const newYear = document.getElementById("newYear").value;
const newRuntime = document.getElementById("newRuntime").value;
const newCast = document.getElementById("newCast").value;
const newPlot = document.getElementById("newPlot").value;
var addNewMovie = {
newTitle : {
rating: newRating,
year: newYear,
runtime: newRuntime,
cast: newCast,
plot: newPlot,
}
};
Object.assign(movieData, addNewMovie)
console.log(movieData);
displayMovieData();
}
form.addEventListener('submit',handleForm);
我已经尝试过了,但不幸的是 newTitle 没有注册并作为字符串传递。
【问题讨论】:
标签: javascript-objects key-value