[英]Appending objects to JSON lists
There is an exception occurring and I am not sure why because I am new to JS. 发生异常,我不确定为什么因为我是JS的新手。 As soon as I reach the line
testData["beacons"].append(beacon);
我一到达
testData["beacons"].append(beacon);
My code jumps to catch(e)
. 我的代码跳到了
catch(e)
。 I am assuming I cant append objects to other arrays? 我假设我不能将对象附加到其他数组?
JS: JS:
$(document).ready(function() {
// Data to describe what kind of test
var testData = {
"timestamp": "",
"hive": 0,
"hdfs": 0,
// Contains a list of testData objects
"beacons":[]
};
var testRun = document.getElementById("test-form");
testRun.addEventListener('submit', function(event) {
event.preventDefault();
var selectedTest = document.querySelector('input[name=test-select]:checked');
alert(selectedTest);
var testType = selectedTest.id;
if (testType == "hdfs-test") {
testData["hdfs"] = 1;
testData["hive"] = 0;
} else if (testType == "hive-test") {
testData["hdfs"] = 0;
testData["hive"] = 1;
} else if (testType == "hdfs-hive-test") {
testData["hdfs"] = 1;
testData["hive"] = 1;
} else {
// null
}
var events = document.getElementById("event-textarea").value;
// check in valid input
var eventSource = events.replace("],[","],,,,[");
// beaconLists allows users to submit --> [{beacon1}, {beacon2}, ...], [{beacon3}, {beacon4}, ...]
var beaconLists = eventSource.split(",,,,");
for (var i = 0; i < beaconLists.length; i++) {
// inspect one list in beaconLists [{beacon1}, {beacon2}, ...]
var beaconList = beaconLists[i];
try {
// list of JSON objects
var beaconObjList = JSON.parse(beaconList);
for (var j = 0; j < beaconObjList.length; j++) {
var beaconObj = beaconObjList[j];
if (beaconObj["data"] && beaconObj["application"]) {
// successful parse to find events
// describe beacon being tested
alert("yes");
var beacon = {
"app_name": beaconObj["application"]["app_name"],
"device": beaconObj["application"]["device"],
"device_id": beaconObj["application"]["device_id"],
"os": beaconObj["application"]["os"],
"os_version": beaconObj["application"]["os_version"],
"browser": beaconObj["application"]["browser"],
"beacon": beaconObj
};
// append to testData
testData["beacons"].append(beacon);
// reset beacon so we can append new beacon later
beacon = {};
} else {
// notify event isn't in the correct format?
alert("no");
}
}
} catch (e) {
// notify bad JSON
alert("failed");
}
}
console.log(testData);
//$.ajax({
// type: "POST",
// url: "/test/",
// data: testData,
// success: function () {
// alert("yay");
// },
// failure: function () {
// alert("boo");
// }
//});
});
});
There is nothing wrong with having an array of objects. 拥有一个对象数组没有任何问题。 JavaScript handles that just fine.
JavaScript处理就好了。 The main issue is that
append
is a jQuery API method for adding elements (psuedo native appendChild
). 主要问题是
append
是一个用于添加元素的jQuery API方法(psuedo native appendChild
)。 push
is how you add to an array. push
是你添加到数组的方式。
testData["beacons"].push(beacon);
Further, this part of your code is problematic. 此外,这部分代码存在问题。
// reset beacon so we can append new beacon later
beacon = {};
Both the variable beacon
and the one added here testData["beacons"]
are the same. 变量
beacon
和此处添加的testData["beacons"]
都是相同的。 In JavaScript, the value of testData["beacons"]
's recent beacon
is the same as the variable beacon
. 在JavaScript中,
testData["beacons"]
最近的beacon
值与变量beacon
相同。 When the value in the variable beacon
is set to {}
, so is the array's value. 当变量
beacon
值设置为{}
,数组的值也是如此。 This line of code simply needs to be removed. 只需要删除这行代码。 Inside of the variable environment set up, the use of
var
will set up a new variable for beacon
each iteration. 在变量环境设置中,
var
的使用将为每次迭代的beacon
设置一个新变量。
您应该使用push方法,例如:
testData["beacons"].push(beacon);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.