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

I'm trying to store a velocity vector declared as this:me.vel = [0, 0, 0];into a MySQL database. I

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]);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742303525a4418485.html

相关推荐

  • CentOS 7编译安装Boost

    在CentOS 7上编译和安装Boost C++库需要执行一系列步骤。Boost是一个强大的C++库集,提供了许多有用的工具和数据结构,但在某些情况下,你可能需要手动编译和安装它。以下是详细的步骤:1. 安装编译工具和依赖项:在开始之前,确

    3小时前
    00
  • git pull and git fetch到底有什么区别?

    git pull 和 git fetch 都是Git版本控制系统中用于获取远程仓库更新的命令,但它们有一些关键区别:git fetch:git fetch 用于从远程仓库下载(拉取)最新的提交和分支信息,但它不会自动合并这些变更到当前工作

    2小时前
    10
  • centos下安装elasticsearch

    在CentOS下安装Elasticsearch Head(也称为Elasticsearch HQ)是一种可视化管理工具,用于监控和管理Elasticsearch集群。以下是安装Elasticsearch Head的步骤:注意:Elastic

    2小时前
    10
  • JavaScript的基本知识点解析

    JavaScript是一种高级编程语言,广泛用于网页开发和构建现代Web应用程序。它是Web前端开发的核心技术之一,具有许多重要的基本知识点。以下是对JavaScript基本知识点的详细解析:变量与数据类型:JavaScript中的变量用

    2小时前
    10
  • 【动态规划篇】

    62. 不同路径题目链接: 62. 不同路径题目解析: 状态表示dp[i][j]表示:以[i][j]为终点时,一共有多少种路径。状态转移方程以[i][j]最近的几步来分析问题,要么从[i-1][j]位置向下走一步到达[i][j],要么从[i

    2小时前
    00
  • 【redis】缓存 更新策略(定期、实时生存),缓存预热、穿透、雪崩、击穿详解

    什么是缓存redis 最常用的场景核心思路就是把一些常用的数据,放到触手可及(访问速度更快)的地方 ⽐如我需要去⾼铁站坐⾼铁. 我们知道坐⾼铁是需要反复刷⾝份证的 (进⼊⾼铁站, 检票, 上⻋,乘⻋过程中, 出站…)正常来说, 我的⾝份证是

    2小时前
    00
  • 【Linux篇】Git和GDB深度剖析:让Linux开发更高效(下篇)

    调试与版本控制的艺术:深入探索Linux开发工具Git与GDB接上篇 ->【Linux篇】提高效率,解决一切问题:Linux 开发者的秘密武器(中篇):本篇博客介绍了在 Linux 环境中,如何使用 makemakefile 工具进

    1小时前
    00
  • 在VMware虚拟机中安装deepin25 Alpha

    1. 前言最近deepin25发布了alpha版本,夜梦闲来无事,机械硬盘正好也有空,便下载了一个玩玩。官网镜像下载:.iso。官网下载镜像的速度可能有些慢,夜梦推荐使用清华源进行下载:.iso相较于deepin23的正式版,deepin2

    1小时前
    10
  • 让可穿戴设备更“贴心”:用户体验设计的奥秘

    让可穿戴设备更“贴心”:用户体验设计的奥秘作者:Echo_Wish在智能手表、智能眼镜、智能戒指等可穿戴设备已经成为日常标配的今天,用户体验(UX,User Experience)设计的重要性不言而喻。一款可穿戴设备如果仅仅是“炫酷的硬

    1小时前
    10
  • AI 实时流量分析:运维老司机的“天眼”系统

    AI 实时流量分析:运维老司机的“天眼”系统1. 前言运维人员最害怕的事情是什么?不是服务器宕机(因为有高可用),也不是 CPU 飙升(因为可以扩容),而是突发流量异常——网站突然卡成PPT,业务瘫痪黑客流量猛如虎,DDoS攻击防不胜防服务

    1小时前
    10
  • harmony OS NEXT–状态管理器–@State详解

    鸿蒙Harmony--状态管理器--@State详解1.1 定义@State装饰的变量,或者称为状态变量,一旦变量拥有了状态属性,就可以触发其直接绑定UI组件的刷新。当状态改变时,UI会发生对应的渲染变化,@State装饰的变量,与声明

    1小时前
    00
  • 让DeepSeek模仿曹操,果然好玩!

    上回说到,在《新三国》中荀彧对曹操说的那句名言,但相比荀彧而言,我觉得曹操的名言会更多,我一想,若能用AI重现这位乱世奸雄曹操,会得到怎样的体验?于是这篇文章我们将以Go语言为例,展示如何通过LangChain框架调用DeepSeek大模型

    56分钟前
    00
  • DeepSeek与全球AI格局:中国技术实力的新象征

    DeepSeek与全球AI格局:中国技术实力的新象征1. 全球AI格局概览大语言模型(LLM)已成为人工智能领域的战略高地,美国科技巨头长期占据主导地位。OpenAI的GPT系列、Anthropic的Claude、Google的Gemini

    49分钟前
    00
  • 装饰器模式:如何用Java打扮一个对象?

    引言在生活中,我们都知道一句话,“人靠衣装马靠鞍”,如果想要让自己在别人眼里看起来更加好看,更加丰富多彩,就得要学会打扮自己,为自己化妆,为自己穿好看的衣服,学会了打扮的本领,那么我们就可以轻松应对不同场合的需求。无论是日常通勤的简约风,还

    43分钟前
    00
  • 「不再手忙脚乱!」用 AI 打造你的专属智能时间管理助手

    摘要在快节奏的生活和工作中,时间管理变得越来越重要,但传统工具往往千篇一律,难以真正贴合个人需求。本文将深入分析传统时间管理的痛点,并结合 Notion AI 和 ChatGPT,打造一个能自动解析任务、优化日程安排、实时同步到 Notio

    41分钟前
    00
  • Google 推出 Gemini 2.5 Pro:提升推理与编程能力

    Google 近日发布了 Gemini 2.5 Pro,这款升级版 AI 模型在推理能力、代码生成和多模态处理方面表现更强。该模型在 LMArena(衡量 AI 回答质量的人类偏好基准)中排名第一,并在数学、科学和逻辑推理等任务上取得了优异

    38分钟前
    00
  • C++20 无序关联容器中的异构查找

    C++20 引入了对无序关联容器(如 std::unordered_map 和 std::unordered_set)的异构查找支持,这一特性极大地提升了查找效率,特别是在处理不同类型键值时。本文将详细介绍这一特性及其带来的性能优势和应用场

    31分钟前
    00
  • Hadoop生态系统:从小白到老司机的入门指南

    Hadoop生态系统:从小白到老司机的入门指南1. 前言:Hadoop到底是个啥?说到大数据,很多人第一时间想到的就是Hadoop。但Hadoop到底是个啥?简单来说,它是一个用来存储和处理大规模数据的分布式系统,适用于海量数据处理场景。很

    24分钟前
    00
  • Intel E810ICE DPU RDMA 及MLX中断原理分析2(CEAE)

    接上文: 创建CQ时的comp_vector参数代码语言:javascript代码运行次数:0运行复制struct ibv_cq *ibv_create_cq(struct ibv_context *context, int cqe,voi

    18分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信