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

标签 javascript arrays loops object conditional-statements

给定以下数据库

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/

相关文章:

c - 对 2D 阵列的一部分进行无限循环

c++ - 如何找到数字的反对数(以 2 为底)?

javascript - 将 appendTo 动态行值传递给 Bootstrap 模式

javascript - 为什么 Javascript 数组可以同时保存多种数据类型?

javascript - 如何向 json 添加新值

javascript - 在 JavaScript 中删除数组内容时规避引用

sql-server - T-SQL 层次结构查询

javascript - 没有嵌入 API 访问 - iframely

javascript - 处理大型 JSON 文件的最佳实践

javascript - 在D3中绘制分层圆弧