admin管理员组文章数量:1386647
I am trying to pass the value of all the textareas on a page to a JSON table. For some reason I get a "cannot set value of undefined error", I dont understand since my Json is defined earlier.
Here is the code with the 2 functions.
function parseToJson() {
var json = {};
$("input.invoice_details").each(function () {
json[this.id] = $(this).val();
});
$("select.invoice_details").each(function () {
json[this.id] = $(this).val();
});
json.categories = {};
$("textarea.invoice_categories").each(function () {
if (this.value.trim() !== '') {
json.categories[this.id].values = splitInput(this);
json.categories[this.id].label = this.name;
}
});
console.log(JSON.stringify(json));
generatePreveiw(json);
}
function splitInput(textArea) {
var input = textArea.value.trim();
var lines = input.split("\n");
var array = [];
$.each(lines, function (indexLine, line) {
var columns = line.split("\t");
var obj = {};
$.each(columns, function (indexColumn, column) {
var columnName = columnsName.columnsName[indexColumn].name;
obj[columnName] = column;
});
array.push(obj);
});
return array;
}
I am trying to pass the value of all the textareas on a page to a JSON table. For some reason I get a "cannot set value of undefined error", I dont understand since my Json is defined earlier.
Here is the code with the 2 functions.
function parseToJson() {
var json = {};
$("input.invoice_details").each(function () {
json[this.id] = $(this).val();
});
$("select.invoice_details").each(function () {
json[this.id] = $(this).val();
});
json.categories = {};
$("textarea.invoice_categories").each(function () {
if (this.value.trim() !== '') {
json.categories[this.id].values = splitInput(this);
json.categories[this.id].label = this.name;
}
});
console.log(JSON.stringify(json));
generatePreveiw(json);
}
function splitInput(textArea) {
var input = textArea.value.trim();
var lines = input.split("\n");
var array = [];
$.each(lines, function (indexLine, line) {
var columns = line.split("\t");
var obj = {};
$.each(columns, function (indexColumn, column) {
var columnName = columnsName.columnsName[indexColumn].name;
obj[columnName] = column;
});
array.push(obj);
});
return array;
}
Share
Improve this question
edited Oct 21, 2015 at 9:45
Rory McCrossan
338k41 gold badges320 silver badges351 bronze badges
asked Oct 21, 2015 at 9:32
0xtuytuy0xtuytuy
1,6547 gold badges29 silver badges56 bronze badges
4
- Which line throws the error? – JJJ Commented Oct 21, 2015 at 9:36
- json.categories[this.id].values = splitInput(this); – 0xtuytuy Commented Oct 21, 2015 at 9:42
- Please show your json data – Jibin Mathew Commented Oct 21, 2015 at 9:47
-
1
var json = {};
— That isn't JSON. That is a JavaScript object. JSON is an external data format that happens to be inspired by JavaScript literal syntax. – Quentin Commented Oct 21, 2015 at 9:48
3 Answers
Reset to default 4 json.categories[this.id].values = splitInput(this);
json.categories[this.id].label = this.name;
should be:
json.categories[this.id] = {
values: splitInput(this),
label: this.name
};
You can't set the properties of an object when you haven't created the object first. You can use this object literal syntax to create the object and its properties in one step.
json.categories = {};
You have an empty object
json.categories[this.id].values = splitInput(this);
Now you are trying to access a property called this.id
from that object.
Since the object doesn't have any properties yet, that will always be undefined
You then try to assign undefined.value = splitInput(this)
, which throws an error.
You need to make sure json.categories[this.id]
has a value before you can set properties on it.
The problem is that you are trying to access a property of the object stored in json.categories[this.id]. Unfortunately it is undefined. Try the following:
function() {
if (this.value.trim() !== '')
{
//Initialize to an empty object if necessary
json.categories[this.id] = json.categories[this.id] || {};
json.categories[this.id].values = splitInput(this);
json.categories[this.id].label = this.name;
}
});
本文标签: javascriptJSON Uncaught TypeError Cannot set property 39values39 of undefinedStack Overflow
版权声明:本文标题:javascript - JSON: Uncaught TypeError: Cannot set property 'values' of undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744533906a2611198.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论