数组被覆盖(Javascript)

时间:2015-05-31 14:31:50

标签: javascript arrays

我正在制作SPA,让我们可以搜索电影片名和剧情名称(研究项目)。好吧,我遇到了一个奇怪的问题。不幸的是,当我尝试将2个表(scenarists和电影)连接成一个(所以我可以获得许多连接)时,创建的新数组总是被最后创建的对象覆盖。我找不到问题,如果有人可以提供帮助,我会很高兴。这个问题介于push methond和respond.json(电影)之间。

var express = require('express');
var bodyParser = require('body-parser');
var port = +process.argv[2];

var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

var movieID = 11; //first free movie's id
var scenaristID = 15; // first free scenarist's id
var scenarists = [ { id: 1, name: "Glenn Ficarra"},
              { id: 2, name: "John Requa"},
              { id: 3, name: "Jakub Kos"},
              { id: 4, name: "David Kos"},
              { id: 5, name: "Jonathan Nolan"},
              { id: 6, name: "Christopher Nolan"},
              { id: 7, name: "Jeb Stuart"},
              { id: 8, name: "Steven E. de Souza"},
              { id: 9, name: "Doug Richardson"},
              { id: 10, name: "Jason Hall"},
              { id: 11, name: "Noah Oppenheim"},
              { id: 12, name: "T.S Nowlin"},
              { id: 13, name: "Luc Besson"},
              { id: 14, name: "Michael Allin"}];

var movies = [ { id: 1, title: "Focus", scenarist1: 1, scenarist2: 2},
          { id: 2, title: "Test Drive", scenarist1: 3, scenarist2: 4},
          { id: 3, title: "Interstellar", scenarist1: 5, scenarist2: 6},
          { id: 4, title: "Die Hard", scenarist1: 7, scenarist2: 8},
          { id: 5, title: "Die Hard 2", scenarist1: 8, scenarist2: 9},
          { id: 6, title: "The Dark Knight Rises", scenarist1: 5, scenarist2: 6},
          { id: 7, title: "American Sniper", scenarist1: 10, scenarist2: -1},
          { id: 8, title: "The Maze Runner", scenarist1: 11, scenarist2: 12},
          { id: 9, title: "Taxi", scenarist1: 13, scenarist2: -1},
          { id: 10, title: "Enter The Dragon", scenarist1: 14, scenarist2: -1}];

movieCollection = new Array();

//GET - pobranie kolekcji
app.get('/movies', function (request, respond) {
    var i, scen1, scen2, scenarists, movie, movie2;
    console.log('reading movies');
    movie = { title: undefined,
             scenarist1: undefined,
             scenarist2: undefined,
             id: undefined
            };

    for (i = 0; i < movies.length; i++) {
        scen1 = movies[i].scenarist1;
        scen2 = movies[i].scenarist2;
        scenarists = connectScenarist(scen1, scen2);
        console.log('scenarists ' + i + ' = ' + scenarists.scenarist1 + ', ' + scenarists.scenarist2);
        movie.scenarist1 = scenarists.scenarist1;
        movie.scenarist2 = scenarists.scenarist2;
        movie.id = movies[i].id;
        movie.title = movies[i].title;
        movieCollection.push(movie);
        //console.log(movieCollection[i].scenarist1);
        //console.log(JSON.stringify(movieCollection));
    }
    //console.log(JSON.stringify(movieCollection));
    console.log(movieCollection[2].scenarist1);
    respond.json(movieCollection);
});

function connectScenarist(sce1, sce2) {
    var i, counter, allScenarists;
    counter = 0;
    allScenarists = {
        scenarist1: undefined,
        scenarist2: undefined
    };
    if (sce1 === -1) {
        allScenarists.scenarist1 = "-";
        counter++;
    }
    if (sce2 === -1) {
        allScenarists.scenarist2 = "-";
        counter++;
    }
    if (counter < 2) {
        for (i = 0; i < scenarists.length; i++) {
            if (sce1 === scenarists[i].id) {
                allScenarists.scenarist1 = scenarists[i].name;
                counter++;
            }
            if (sce2 === scenarists[i].id) {
                allScenarists.scenarist2 = scenarists[i].name;
                counter++;
            }
            if (counter === 2) {
                break;
            }
        }
    }
    //console.log('Saving movie: ' + JSON.stringify(allScenarists));
    return allScenarists;
}

2 个答案:

答案 0 :(得分:0)

你可以试试jQuery的扩展

试试这个:

var object = $.extend({}, object1, object2);

答案 1 :(得分:0)

是的,您的函数中只有一个movie对象。在分配或在JavaScript中调用时,不会复制对象。通过将文字放在循环体中,在每次迭代时创建一个新的!

此外,您的应用中有一个由所有请求共享的movieCollection数组。不完全是你想要的,得到其他用户的结果。

var movieCollection = [];
for (var i = 0; i < movies.length; i++) {
    var m = movies[i],
        scenarists = connectScenarist(m.scenarist1, m.scenarist2);
    movieCollection.push({
        title: m.title,
        scenarist1: scenarists.scenarist1,
        scenarist2: scenarists.scenarist2,
        id: m.id
    });
    console.log(movieCollection[i]);
}
console.log(JSON.stringify(movieCollection));