最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I convert a velocity array [x, y, z] into a string to be stored in a database? - Stack Overflow

programmeradmin1浏览0评论

I'm trying to store a velocity vector declared as this:

 me.vel = [0, 0, 0];

into a MySQL database. I assume this needs to be converted into a string before it can be stored, but that's because I don't know of any appropriate type to store it as, ex: VARCHAR, INT, STRING, etc. (If it can be stored let me know which type as this would be a simpler solution and I wouldn't have to convert into a string then back to a vector)

I've tried:

 var velocityString = me.vel.join();
 var velocityString = String(me.vel);

but those don't seem to work.

How can I convert this array into a string?

Thanks, Digimas

I'm trying to store a velocity vector declared as this:

 me.vel = [0, 0, 0];

into a MySQL database. I assume this needs to be converted into a string before it can be stored, but that's because I don't know of any appropriate type to store it as, ex: VARCHAR, INT, STRING, etc. (If it can be stored let me know which type as this would be a simpler solution and I wouldn't have to convert into a string then back to a vector)

I've tried:

 var velocityString = me.vel.join();
 var velocityString = String(me.vel);

but those don't seem to work.

How can I convert this array into a string?

Thanks, Digimas

Share Improve this question asked May 27, 2011 at 14:55 user671891user671891 1651 gold badge3 silver badges14 bronze badges 3
  • string.concat(str1, str2, ..., strN) may do the trick -- but you're going to have trouble unpacking it. – Joseph Weissman Commented May 27, 2011 at 14:57
  • Why is everyone agreeing with storing numbers as strings? – Michael Mior Commented May 27, 2011 at 15:03
  • Because that's the question that was asked and I'm not an expert on MySQL's datatypes. – lawnsea Commented May 27, 2011 at 15:07
Add a ment  | 

6 Answers 6

Reset to default 2

Or you could json encode the object.

Why not store the data as three separate columns? vel_x, vel_y, vel_z (perhaps a DOUBLE). This is much cleaner and will save you the trouble of converting to/from strings all the time.

If (and I don't think it's the case) the velocity element values are fixed, you could use one of the MySQL SET or ENUM datatypes to store your Array.

Otherwise I'd go for JSON and store a JSON.encoded string in a VARCHAR field (unless you're storing light velocities)1:

/* Store:    */ JSON.encode([1,2,3]);            //=> "[1,2,3]"
/* Retrieve: */ JSON.parse(velocityFromSQL); //=> [1,2,3]

1 VARCHAR can contain up to 255 characters

Both of the solutions you tried should work. I get "0,0,0" for both. But as Christopher suggested before, JSON encode would work, and in my opinion is the best solution. joining or stringifying just create a ma delimited list, which with a single example as this would be ok. But if you do anything more plex you rist loosing meaning from your data (say if you array wasn't only numbers.) So if JSON.stringify/parse are available use that, otherwise [0,0,0].join(",")/"0,0,0".split(",") will work.

I would do var velString = me.vel.join(','); if you want it to be a string.

To convert it back:

var vel = velString.split(',');

for (var i = 0; i < vel.length; i++) {
    vel[i] = parseFloat(vel[i]);
}

Alternatively, if you know that your users will only be using modern browsers:

var vel = velString.split(',').map(parseFloat)

I can't ment on a more appropriate datatype in MySQL. It seems like three numeric columns might make more sense unless you expect to record higher-dimension velocities in the future.

var vel = [0, 0, 0];
var vel_string = vel.join('|');

Gives you 0|0|0

Then to make it an array if you have the string outta the database:

var vel_string = '0|0|0';
var vel = vel_string.split('|');
for(var i in vel) {
    vel[i] = parseInt(vel[i]);
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论