Javascript JSON.stringify(array) 用方括号括起来
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29737024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON.stringify(array) surrounded with square brackets
提问by Devcon
I'm trying to get an array of data to insert into multiple columns in an sqlite database, I'v got it almost working with this:
我正在尝试将一组数据插入到 sqlite 数据库中的多列中,我几乎可以使用它:
function InsertData(dbData){
var valueArray = [], dataArray = [];
var values = Object.keys(dbData);
for(var key in values){
valueArray[valueArray.length] = values[key];
dataArray[dataArray.length] = dbData[values[key]];
}
console.log("INSERT INTO "+dbData.table+" ("+valueArray+") VALUES ("+JSON.stringify(dataArray)+")");
dbData.database.serialize(function(){
dbData.database.run("INSERT INTO "+dbData.table+" ("+valueArray+") VALUES ("+JSON.stringify(dataArray)+")");
});
}
My data is constructed as so:
我的数据是这样构建的:
//app.js
function EventData(title, value, other){
this.title = title;
this.value = value;
this.other = other;
}
EventData.prototype = new dbtools.DBData(usuevents, 'event');
var thisEventData = new EventData('newData', 4, 2);
//dbtools.js
DBData = function(database, table){
this.database = database;
this.table = table;
};
the console.log outputs INSERT INTO event (title,value,other) VALUES (["newData",4,2])I just need the [] square brackets removed on (["newData",4,2]). How would I do this? Also if there is a better way to insert data into sqlite im open to suggestions. Thanks
console.log 输出INSERT INTO event (title,value,other) VALUES (["newData",4,2])我只需要删除(["newData",4,2])上的 [] 方括号。我该怎么做?此外,如果有更好的方法将数据插入到sqlite,我愿意接受建议。谢谢
回答by Drakes
JSON.stringify(dataArray)will always return square brackets because dataArrayis of type array. Instead, you could stringify each element of your array, then join them separated by commas.
JSON.stringify(dataArray)将始终返回方括号,因为它dataArray是数组类型。相反,您可以对数组的每个元素进行字符串化,然后将它们连接起来,以逗号分隔。
dataArray = dataArray.map(function(e){
return JSON.stringify(e);
});
dataString = dataArray.join(",");
Then
然后
["newData",4,2]
becomes
变成
"newData",4,2
Addendum:Please don't try to replace()out the brackets manually because it could be that one day a bracket enters your array in a string. e.g. ["Please see ref[1]",4,2]
附录:请不要尝试replace()手动去掉括号,因为有一天括号可能会以字符串形式输入您的数组。例如["Please see ref[1]",4,2]
回答by S McCrohan
Drake's answer would be my personal choice, because the code clearly expresses the intent. But to show an alternative:
Drake 的答案将是我个人的选择,因为代码清楚地表达了意图。但要显示一个替代方案:
var strung = JSON.stringify(dataArray);
console.log(strung.substring(1,strung.length-1));
回答by Devcon
Thanks Drakes and S McCrohan for your answers. I found that .replace(/]|[[]/g, '')appended to JSON.stringify(dataArray)
感谢 Drakes 和 S McCrohan 的回答。我发现.replace(/]|[[]/g, '')附加到JSON.stringify(dataArray)
full line JSON.stringify(dataArray).replace(/]|[[]/g, '')
全线 JSON.stringify(dataArray).replace(/]|[[]/g, '')

