我刚开始使用Arrays,Objects和JSON - 所以希望我能在这里看到一些简单的东西。我在尝试将新项目添加(推送)到我的json对象时遇到错误。
我遇到以下错误:Result of expression 'library.push' [undefined] is not a function
(在我的代码段底部)。
// 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]) {
// Replace values with new
library[title].foregrounds = foregrounds;
library[title].backgrounds = backgrounds;
// Save to Database. Then on callback...
document.write('Changes Saved to <b>'+title+'</b>');
// 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...
document.write('Added: <b>'+title+'</b>');
}
}
save();
答案 0 :(得分:45)
library
是一个对象,而不是一个数组。你把东西推到了数组上。与PHP不同,Javascript有所区别。
您的代码尝试创建一个看起来像键值对的源代码的字符串,然后将其“推”到该对象上。这甚至不及它如何运作。
您要做的是向对象添加新的键值对,其中键是标题,值是另一个对象。看起来像这样:
library[title] = {"foregrounds" : foregrounds, "backgrounds" : backgrounds};
“JSON对象”是一个模糊的术语。您必须小心区分程序内存中的实际对象和JSON格式的文本片段。
答案 1 :(得分:12)
如果您的JSON没有密钥,您可以这样做:
library[library.length] = {"foregrounds" : foregrounds,"backgrounds" : backgrounds};
所以,试试这个:
var library = {[{
"title" : "Gold Rush",
"foregrounds" : ["Slide 1","Slide 2","Slide 3"],
"backgrounds" : ["1.jpg","","2.jpg"]
}, {
"title" : California",
"foregrounds" : ["Slide 1","Slide 2","Slide 3"],
"backgrounds" : ["3.jpg","4.jpg","5.jpg"]
}]
}
然后:
library[library.length] = {"title" : "Gold Rush", "foregrounds" : ["Howdy","Slide 2"], "backgrounds" : ["1.jpg",""]};
答案 2 :(得分:8)
push是一个Array方法,对于json对象,您可能需要定义它
这应该这样做:
library[title] = {"foregrounds" : foregrounds,"backgrounds" : backgrounds};
答案 3 :(得分:0)
您可以使用 Lodash _.assign
函数来实现。
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 <b>' + title + '</b>');
}
// 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: <b>' + title + '</b>');
}
}
save();
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>