I want to merge two JSON files that contain information about movies. These files have some items in common. I want to write a third file which includes all the movies without repeating them.
Here's what I have so far:
const fs = require('fs');
const path = require('path');
const readMovies1 = () => {
return new Promise((resolve, reject) => {
fs.readFile(path.join(__dirname,'../models/movies.json'), 'utf8', (err, data1) => {
if(err) reject(err);
let data = JSON.parse(data1);
resolve(data);
});
});
};
const readMovies2 = (data1) => {
return new Promise((resolve, reject) =>{
fs.readFile(path.join(__dirname,'../models/movies2.json'), 'utf8', (err, data) => {
if(err) reject(err);
data = JSON.parse(data);
resolve([data1,data]);
});
});
};
const merging = (data1, data2) => {
var args = arguments;
var hash = {};
var arr = [];
for (var i = 0; i < args.length; i++) {
for (var j = 0; j < args[i].length; j++) {
if (hash[args[i][j]] !== true) {
arr[arr.length] = args[i][j];
hash[args[i][j]] = true;
}
}
}
return arr;
};
readMovies1()
.then(readMovies2)
.catch((err) => console.error(err))
.then((data) => console.log(merging(data[0],data[1])))
.catch((err) => console.error(err));
But the console output gives me this:
[ undefined,
'/',
'U',
's',
'e',
'r',
'g',
'o',
'n',
'z',
'P',
'j',
'c',
't',
'J',
'S',
'a',
'd',
'm',
'-',
'v',
'i',
'l',
'b',
'.' ]
I found the merging function in a question here on stack overflow but it was merging an array of numbers, and I am merging arrays of objects. I don't know if that has something to do with my problem.
Here's one of my source files (the other follows the same pattern but has some other movies):
[{
"title": "Spider-Man: Homecoming",
"usersScore": "92%",
"criticsScore": "89%"
}, {
"title": "Girls Trip",
"usersScore": "89%",
"criticsScore": "83%"
}, {
"title": "Captain Underpants: The First Epic Movie (Captain Underpants)",
"usersScore": "87%",
"criticsScore": "62%"
}, {
"title": "Guardians of the Galaxy Vol. 2",
"usersScore": "82%",
"criticsScore": "88%"
}, {
"title": "Wonder Woman",
"usersScore": "92%",
"criticsScore": "89%"
}, {
"title": "First They Killed My Father",
"usersScore": "88%",
"criticsScore": "83%"
}, {
"title": "Baby Driver",
"usersScore": "93%",
"criticsScore": "87%"
}, {
"title": "Demon",
"usersScore": "91%",
"criticsScore": "56%"
}, {
"title": "The Music of Strangers: Yo-Yo Ma and the Silk Road Ensemble",
"usersScore": "84%",
"criticsScore": "85%"
}, {
"title": "Colossal",
"usersScore": "80%",
"criticsScore": "59%"
}, {
"title": "Certain Women",
"usersScore": "92%",
"criticsScore": "51%"
}, {
"title": "Godzilla Resurgence (Shin Godzilla)",
"usersScore": "84%",
"criticsScore": "73%"
}, {
"title": "My Cousin Rachel",
"usersScore": "76%",
"criticsScore": "47%"
}, {
"title": "The Meyerowitz Stories (New and Selected)",
"usersScore": "93%",
"criticsScore": "84%"
}, {
"title": "Raw",
"usersScore": "90%",
"criticsScore": "77%"
}, {
"title": "The Wedding Plan",
"usersScore": "86%",
"criticsScore": "65%"
}, {
"title": "Maudie",
"usersScore": "88%",
"criticsScore": "92%"
}, {
"title": "Heal the Living (Réparer les vivants)",
"usersScore": "90%",
"criticsScore": "70%"
}, {
"title": "Lady Macbeth",
"usersScore": "89%",
"criticsScore": "72%"
}, {
"title": "The Exception (The Kaiser's Last Kiss)",
"usersScore": "76%",
"criticsScore": "67%"
}, {
"title": "Citizen Jane: Battle for the City",
"usersScore": "94%",
"criticsScore": "61%"
}, {
"title": "The Beguiled",
"usersScore": "78%",
"criticsScore": "50%"
}, {
"title": "The Big Sick",
"usersScore": "98%",
"criticsScore": "89%"
}, {
"title": "The Little Hours",
"usersScore": "77%",
"criticsScore": "53%"
}, {
"title": "A Ghost Story",
"usersScore": "91%",
"criticsScore": "66%"
}, {
"title": "The Hero",
"usersScore": "77%",
"criticsScore": "64%"
}, {
"title": "Megan Leavey",
"usersScore": "84%",
"criticsScore": "83%"
}, {
"title": "Band Aid",
"usersScore": "85%",
"criticsScore": "73%"
}, {
"title": "It Comes At Night",
"usersScore": "89%",
"criticsScore": "43%"
}, {
"title": "The Midwife (Sage femme)",
"usersScore": "86%",
"criticsScore": "82%"
}, {
"title": "Brawl in Cell Block 99",
"usersScore": "93%",
"criticsScore": "75%"
}, {
"title": "Gerald's Game",
"usersScore": "89%",
"criticsScore": "78%"
}]