I\'m just starting with Arrays, Objects, and JSON - so hopefully there\'s just something simple I\'m overlooking here. I\'m encountering an error when attempting to
You can achieve this using Lodash _.assign function.
library[title] = _.assign({}, {'foregrounds': foregrounds }, {'backgrounds': backgrounds });
// This is my JSON object generated from a database
var library = {
"Gold Rush": {
"foregrounds": ["Slide 1", "Slide 2", "Slide 3"],
"backgrounds": ["1.jpg", "", "2.jpg"]
},
"California": {
"foregrounds": ["Slide 1", "Slide 2", "Slide 3"],
"backgrounds": ["3.jpg", "4.jpg", "5.jpg"]
}
}
// These will be dynamically generated vars from editor
var title = "Gold Rush";
var foregrounds = ["Howdy", "Slide 2"];
var backgrounds = ["1.jpg", ""];
function save() {
// If title already exists, modify item
if (library[title]) {
// override one Object with the values of another (lodash)
library[title] = _.assign({}, {
'foregrounds': foregrounds
}, {
'backgrounds': backgrounds
});
console.log(library[title]);
// Save to Database. Then on callback...
// console.log('Changes Saved to ' + title + '');
}
// If title does not exist, add new item
else {
// Format it for the JSON object
var item = ('"' + title + '" : {"foregrounds" : ' + foregrounds + ',"backgrounds" : ' + backgrounds + '}');
// THE PROBLEM SEEMS TO BE HERE??
// Error: "Result of expression 'library.push' [undefined] is not a function"
library.push(item);
// Save to Database. Then on callback...
console.log('Added: ' + title + '');
}
}
save();