I have an object like this:
我有一个像这样的对象:
var jsonObject = Object {transid: "104", amt: "750.00", dt: "2015-04-28 08:12:22", code: "11222", shop: "Joes Cafe"}
I tried converting it to an array like this:
我尝试将它转换为这样的数组:
var jsArray = Object.keys(jsonObject).map(function(k) { return jsonObject[k] });
and I get a result like this:
我得到一个这样的结果:
["104", "750.00", "2015-04-28 08:12:22", "11222", "Joes Cafe"]
[“104”,“750.00”,“2015-04-28 08:12:22”,“11222”,“Joes Cafe”]
But I want jsArray
to be like this:
但我希望jsArray是这样的:
[{transid: "104", amt: "750.00", dt: "2015-04-28 08:12:22", code: "11222", shop: "Joes Cafe"}]
How do I go about it?
我该怎么办呢?
2 个解决方案
#1
You can create an array, and insert your object into it.
您可以创建一个数组,并将对象插入其中。
var array = [];
var jsonObject = {
transid: "104",
amt: "750.00",
dt: "2015-04-28 08:12:22",
code: "11222",
shop: "Joes Cafe"
}
array.push(jsonObject);
document.write(JSON.stringify(array));
#2
It couldn't be simpler.
这简直太简单了。
You actually don't have to convert anything here. You just have to create the array and add the object as element:
你实际上不必在这里转换任何东西。您只需创建数组并将对象添加为元素:
var arr = [obj]; // [ ] denotes an array literal in this case
详细了解阵列。
#1
You can create an array, and insert your object into it.
您可以创建一个数组,并将对象插入其中。
var array = [];
var jsonObject = {
transid: "104",
amt: "750.00",
dt: "2015-04-28 08:12:22",
code: "11222",
shop: "Joes Cafe"
}
array.push(jsonObject);
document.write(JSON.stringify(array));
#2
It couldn't be simpler.
这简直太简单了。
You actually don't have to convert anything here. You just have to create the array and add the object as element:
你实际上不必在这里转换任何东西。您只需创建数组并将对象添加为元素:
var arr = [obj]; // [ ] denotes an array literal in this case
详细了解阵列。