Array overwritten (Javascript) -
i'm working on spa let search through movie titles , scenarists names (an studies project). well, got stuck on weird problem. unfortunately when try connect 2 tables (scenarists , movies) 1 (so can many-many connections) new array gets create overwritten last created object. can't find problem, i'll glad if can help. problem somewhere between push methond , respond.json(movie).
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 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 }; (i = 0; < movies.length; i++) { scen1 = movies[i].scenarist1; scen2 = movies[i].scenarist2; scenarists = connectscenarist(scen1, scen2); console.log('scenarists ' + + ' = ' + 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) { (i = 0; < 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; }
yes, there's single movie
object in function. objects don't copied on assignment or on call in javascript. create new 1 on every iteration putting literal in body of loop!
also, there single moviecollection
array in app shared requests. not want, getting results of other users.
var moviecollection = []; (var = 0; < 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));
Comments
Post a Comment