I think you just need to compare two objects.
Suppose we loaded two objects from files:
var target = JSON.parse(dataDefault); var current = JSON.parse(dataCZ);
Next, we compare all the properties, if there is no such property or its value is equal to the original one, then we write it into the resulting object:
var result = {}; for (var key in target) { if (!target.hasOwnProperty(key)) continue; if (target[key] == current[key] || !current[key]) { result[key] = target[key]; } }
Next, save:
fs.writeFile("result.json", JSON.stringify(result));
Update:
I see you have a complex structure, you need to walk through all the nested values recursively. You can add the following function:
function compare(target, current){ var result = {}; for (var key in target) { if (!target.hasOwnProperty(key)) continue; if(typeof target[key] == "object"){ result[key] = compare(target[key], current[key]) } else { if (target[key] == current[key] || !current[key]) { result[key] = target[key]; } } } return result; }
And call:
fs.writeFile("result.json", JSON.stringify(compare(target, current)));
var target = { "Shell" : { "Profile" : "Profile", "SignOut" : "Sign out", "CloseView" : "Close current view", "Settings" : "Settings", "ModifySettings" : "Modify settings", "SettingsModified" : "Settings have been changed", "Help" : "Help" } } var current = { "Shell" : { "Profile" : "Профиль", "SignOut" : "Выйти", "CloseView" : "", "Settings" : "Settings", "ModifySettings" : "Дополнительные настройки", "Help" : "Помощь" } }; function compare(target, current) { var result = {}; for (var key in target) { if (!target.hasOwnProperty(key)) continue; if (typeof target[key] == "object") { result[key] = compare(target[key], current[key]) } else { if (target[key] == current[key] || !current[key]) { result[key] = target[key]; } } } return result; } console.log(compare(target, current));
Update 2
Your code should look something like this:
// получить третий аргумент из командной строки var name = process.argv[2]; var defaultName = "en_US"; var file = {/*...*/} var nfResult = {}; if(!file[name]){ nfResult = file[name] = name[defaultName]; } else { nfResult = compare(name[defaultName], file[name]); }