gpt4 book ai didi

javascript - 如何将参数插入多重嵌套数组/对象结构中?

转载 作者:行者123 更新时间:2023-12-01 00:38:09 24 4
gpt4 key购买 nike

给定以下数据库

DB = [
{
genre:'thriller',
movies:[
{
title:'the usual suspects', release_date:1999
}
]},
{
genre:'commedy',
movies:[
{
title:'pinapple express', release_date:2008
}
]}
]

我想检查其中是否存在类型和电影,如果不存在则添加。

到目前为止我已经有了这个代码。唯一缺少的是,如果电影不存在(已注释掉并加粗),则将(新)电影推送到类型索引处。

var moviesDB = function (array, genre, movie) {
var x = []

for (var i = 0; i < DB.length; i++) {
x.push(DB[i].genre);
}

if(x.includes(genre) == false) {
DB.push({genre: genre, movies: []});
} else {
console.log("genre already here")
}

var y = []

for (var i = 0; i < DB.length; i++) {
DB[i].movies.forEach (function (object){
y.push(object.title)
})
}

if(y.includes(movie) == false) {
//**push movie into the existing object.**
} else {
return `the movie the ${movie} is already in the database!`
}

return DB;
}

Sp moviesDB = function (DB, "drama", "A Drama movie") 应该添加一个新的流派对象(戏剧),并在电影数组中添加一个带有“戏剧电影”的新对象“标题。并且 moviesDB = function (DB, "commedy", "Scary movie") 应该只在现有喜剧流派对象中添加一个带有电影标题的新对象。

DB = [
{
genre:'thriller',
movies:[
{
title:'the usual suspects', release_date:1999
}
]},
{
genre:'commedy',
movies:[
{
title:'pinapple express', release_date:2008
}
]}
]


var moviesDB = function (array, genre, movie) {
var x = []

for (var i = 0; i < DB.length; i++) {
x.push(DB[i].genre);
}

if(x.includes(genre) == false) {
DB.push({genre: genre, movies: []});
} else {
console.log("genre already here")
}

var y = []

for (var i = 0; i < DB.length; i++) {
DB[i].movies.forEach (function (object){
y.push(object.title)
})
}

if(y.includes(movie) == false) {
//**push movie into the existing object.**
} else {
return `the movie the ${movie} is already in the database!`
}

return DB;
}
console.log(moviesDB(DB, "drama", "A drama movie"))

最佳答案

为了达到预期效果,请将以下代码添加到注释部分

if(y.includes(movie) == false) {
//**push movie into the existing object.**
DB.forEach(v => {
if(v.genre === genre){
v.movies.push({title: movie})
}
})
}

说明:如果电影是现有对象,则循环数据库数组并检查类型并推送到电影列表

工作代码供引用

DB = [
{
genre:'thriller',
movies:[
{
title:'the usual suspects', release_date:1999
}
]},
{
genre:'commedy',
movies:[
{
title:'pinapple express', release_date:2008
}
]}
]


var moviesDB = function(DB, genre, movie) {
movie = typeof movie === 'object'? movie.title : movie; // to check whether movie parameter is string or object
var x = []

for (var i = 0; i < DB.length; i++) {
x.push(DB[i].genre);
}

if(x.includes(genre) == false) {
DB.push({genre: genre, movies: []});
} else {
console.log("genre already here")
}

var y = []

for (var i = 0; i < DB.length; i++) {
DB[i].movies.forEach (function (object){
y.push(object.title)
})
}

if(y.includes(movie) == false) {
//**push movie into the existing object.**
DB.forEach(function(object) {
if(object.genre === genre){
object.movies.push({title: movie})
}
})
} else {
return `the movie the ${movie} is already in the database!`
}

return DB;
}

console.log(moviesDB(DB, "drama", "A drama movie"))
console.log(moviesDB(DB, "commedy", "Scary movie"))
console.log(moviesDB(DB, 'commedy', 'pinapple express'))

codepen - https://codepen.io/nagasai/pen/oNvymeB?editors=1010

关于javascript - 如何将参数插入多重嵌套数组/对象结构中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57927792/

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