把Javascript 对象转换为键值对连接符字符串的方法总结
Do you know a fast and simple way to encode a Javascript Object into a No |
|||||||||||||||||
|
like this?
Edit: this one also converts recursive objects (using php "array" notation for the query string)
|
|||||||||||||||||||||
|
jQuery has a function for this, example:
|
|||||||||||||||||||||
|
Edit: I like this one-liner, but I bet it would be a more popular answer if it matched the accepted answer semantically:
|
|||||||||||||||||||||
|
Here's a one liner in ES6:
|
|||||||||||||||||||||
|
With Node.js v6.6.3
Reference: https://nodejs.org/api/querystring.html |
|||||||||||||
|
A small amendment to the accepted solution by user187291:
Checking for hasOwnProperty on the object makes JSLint/JSHint happy, and it prevents accidentally serializing methods of the object or other stuff if the object is anything but a simple dictionary. See the paragraph on for statements in this page: http://javascript.crockford.com/code.html |
|||
Do you need to send arbitrary objects? If so, GET is a bad idea since there are limits to the lengths of URLs that user agents and web servers will accepts. My suggestion would be to build up an array of name-value pairs to send and then build up a query string:
|
|||
use JSON. take a look at this question for ideas on how to implement. |
|||||||||||||||||
|
Here's the coffeescript version of accepted answer. This might save time to someone.
|
|||||
|
Rails / PHP Style Query BuilderThis method converts a Javascript object into a
Example Usage:
|
|||||
|
I suggest using the URLSearchParams interface:
|
|||||
|
If you want to convert a nested object recursively and the object may or may not contain arrays (and the arrays may contain objects or arrays, etc), then the solution gets a little more complex. This is my attempt. I've also added some options to choose if you want to record for each object member at what depth in the main object it sits, and to choose if you want to add a label to the members that come from converted arrays. Ideally you should test if the thing parameter really receives an object or array.
|
|||||
|
Here's a concise & recursive version with Object.entries. It handles arbitrarily nested arrays, but not nested objects. It also removes empty elements:
E.g.:
|
||||
ok, it's a older post but i'm facing this problem and i have found my personal solution.. maybe can help someone else..
|
|||
Addition for accepted solution, this works with objects & array of objects:
Also have added objName if you're using object parameters like in asp.net mvc action methods. |
|||
A little bit look better
|
||||
This one skips null/undefined values
|
|||
The above answers fill not work if you have a lot of nested objects. Instead you can pick the function param from here - https://github.com/knowledgecode/jquery-param/blob/master/jquery-param.js It worked very well for me!
|
|||
I have a simpler solution that does not use any third-party library and is already apt to be used in any browser that has "Object.keys" (aka all modern browsers + edge + ie): In ES5
In ES3
|
|||
Just another way (no recursive object):
|
|||||
|
Refer from the answer @user187291, add "isArray" as parameter to make the json nested array to be converted.
To make the result : staffId=00000001&Detail[0].identityId=123456&Detail[1].identityId=654321
|
https://stackoverflow.com/questions/1714786/query-string-encoding-of-a-javascript-object
把Javascript 对象转换为键值对连接符字符串的方法总结的更多相关文章
- Javascript对象数据类型(键值对)的创建和使用方法
对象数据类型(键值对) 1.创建键值对 var arry={name:"小明",age:20,isgey:falet} // var 键值对名 = {键:值,键:值} 2.增加键值 ...
- JavaScript中对象转换为原始值的规则
JavaScript中对象转换为原始值遵循哪些原则? P52 对象到布尔值对象到布尔值的转换非常简单:所有的对象(包括数字和函数)都转换为true.对于包装对象亦是如此:new Boolean(fal ...
- 关于JavaScript对象的键和值
一个JavaScript对象由键和值组成. 当一个给定键的值被设置为一个字符串.布尔值.数字.数组或对象时,我们把这个键称为属性. 当把键设置为函数时,我们把它叫做方法.
- json转换为键值对辅助类
/// <summary> /// json转换为键值对辅助类 /// </summary> public class JsonParser { private static ...
- 使用Javascript/jQuery将javascript对象转换为json格式数据 - 海涛的CSDN博客 - 博客频道 - CSDN.NET
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- JavaScript —— 对象的取值与赋值
可能是因为用惯了 Java ,对一个对象取值/赋值喜欢用 setXXX() 和 getXXX() . 在 JavaScript 中使用 setValue() 时,遇到了个奇怪的问题,所以查了下 Jav ...
- C# 给枚举定义DescriptionAttribute,把枚举转换为键值对
在C#中,枚举用来定状态值很方便,例如我定义一个叫做Season的枚举 public enum Season { Spring = 1, Summer = 2, Autumn = 3, Winter ...
- .net c#将数据库数据对象转换为实体值对象
using System; using System.Data; namespace Sunlib { public static class DataHelper { //将数据库数据对象转换为实体 ...
- js对象替换键值名称
js对象替换键值名称 将obj中的id和name字段替换分别替换成为“@id”,“@name” 代码如下: let obj = [{id:,name:,name:"李四"}].ma ...
随机推荐
- 51nod 1412
考虑到只与深度和点的个数有关$f[n][d]$ 表示 $n$ 个点,深度为 $d$ 的 $AVL$ 树有多少种 枚举左子树大小为 $i$, 进行转移并且深度为 $logn$ 级别 $f[n][d] = ...
- 【概率论】1-1:概率定义(Definition of Probability)
title: [概率论]1-1:概率定义(Definition of Probability) categories: Mathematic Probability keywords: Sample ...
- Ocelot 网关 和 consul 服务发现
服务发现 Consul 一.安装和启动 下载 [Consul](https://www.consul.io/downloads.html) 下载完成后,解压,只有一个consul.exe,把目录添加到 ...
- kafka - Confluent.Kafka
上个章节我们讲了kafka的环境安装(这里),现在主要来了解下Kafka使用,基于.net实现kafka的消息队列应用,本文用的是Confluent.Kafka,版本0.11.6 1.安装: 在NuG ...
- Leetcode题目338:比特位计数(中等)
题目描述: 给定一个非负整数 num.对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回. 示例 1: 输入: 2 输出: [0,1,1] 示例 ...
- 中国萌兔-月饼工厂流水线 -万圣节萌宠-月饼售罄后续-B站东予薏米
B站(Blibli)up主,东予薏米.下面画的五只兔兔,两只狗狗,一只猫猫都是她家的 啊!有个会画画的主人真是幸福- 蹦迪的那个兔兔头昏脑胀,敷了冰袋和膏药哈哈哈哈,好可爱! 下班了下班了~今天真是太 ...
- PHP学习之观察者模式
<?php //观察者模式涉及到两个类 //男人类 和女朋友类 //男人类对象小明, 女朋友类对象小花.小丽 class Man { //用了存放观察者 protected $observers ...
- VituralBox从零搭建基于CentOS 7(64位)的Kubernetes+docker集群
1. 下载CentOS 7官方minimal镜像 2. 安装VituralBox(Windows 10 64位) 3. 安装Git for windows(Windows 10 64位) 4. 安装V ...
- PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)
1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highway ...
- Maven项目添加阿里云HBase依赖之后第一行才出现红叉报错“Missing artifact jdk.tools:jdk.tools:jar:1.6”的解决办法
首先是从阿里云上下载了一个样例项目"hbase-demo",然后用eclipse打开,此时eclipse会去下载aliyun.hbase依赖. 等待一段时间后,pom.xml却报错 ...