对于数组arr []的每个元素,我需要将此字符串添加到另一个数组中。我这样尝试过:
var arr = ["TagID3", "TagID4", "TagID5", "TagID6", "TagID7", "TagID8", "TagID9"];
var newarr = [];
for (var i = 0; i < arr.length; i++) {
newarr.push({
"id": i,
"valueAxis": i,
"bullet": "round",
"type": "smoothedLine",
"valueField": i
})
}
for (var d = 0; d < newarr.length; d++) {
document.write(newarr[d]);
}
但是它仅显示 [object Object]
答案 0 :(得分:1)
您的代码看起来不错,但是您可以使用JSON.stringify()
将结果写入可读的文档中。这样可以解决您指出的[object Object]
问题:
var arr = ["TagID3", "TagID4", "TagID5", "TagID6", "TagID7", "TagID8", "TagID9"];
var newarr = [];
for (var i = 0; i < arr.length; i++) {
newarr.push({
"id": i,
"valueAxis": i,
"bullet": "round",
"type": "smoothedLine",
"valueField": i
})
}
/*
for (var d = 0; d < newarr.length; d++) {
// Use JSON.stringify to produce a string from JSON object
// that will write to document in readable way
var stringValue = JSON.stringify(newarr[d])
document.write(stringValue);
}
*/
// Write item data from in newarr as comma separated, readable string
document.write(newarr.map(JSON.stringify).join());