Javascript 对象到数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6215171/
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
Javascript Object to Array
提问by Speedy
I'm having some problems with converting some JSON and I'm after some help. This is my problem, I have JSON returned the following:
我在转换一些 JSON 时遇到了一些问题,我需要一些帮助。这是我的问题,我让 JSON 返回以下内容:
Example of the JSON recieved (from a CSV file):
收到的 JSON 示例(来自 CSV 文件):
[
{
"rating": "0",
"title": "The Killing Kind",
"author": "John Connolly",
"type": "Book",
"asin": "0340771224",
"tags": "",
"review": "i still haven't had time to read this one..."
},
{
"rating": "0",
"title": "The Third Secret",
"author": "Steve Berry",
"type": "Book",
"asin": "0340899263",
"tags": "",
"review": "need to find time to read this book"
},
cut for brevity
]
Now, this is a one dimensional array of objects, but I have a function that I need to pass this to that will ONLY take a multidimensional array. There's nothing I can change on this. I've been looking around the web for conversion and came across this code:
现在,这是一个一维对象数组,但我有一个函数,我需要将它传递给它,该函数只需要一个多维数组。对此,我没有什么可以改变的。我一直在网上寻找转换并遇到了以下代码:
if (! obj.length) { return [];} // length must be set on the object, or it is not iterable
var a = [];
try {
a = Array.prototype.slice.call(obj, n);
}
// IE 6 and posssibly other browsers will throw an exception, so catch it and use brute force
catch(e) {
Core.batch(obj, function(o, i) {
if (n <= i) {
a[i - n] = o;
}
});
}
return a;
But my code keeps getting stuck on the "no object length" part. When I iterate through each object, I get character by character. Unfortunately, those field names (rating, title, author), etc are not set in stone and I can't access anything using the obj.Field notation.
但是我的代码一直卡在“无对象长度”部分。当我遍历每个对象时,我会逐个字符地获取。不幸的是,这些字段名称(评级、标题、作者)等并不是一成不变的,我无法使用 obj.Field 表示法访问任何内容。
I'm stuck on this; is there a way to convert those objects into arrays, or do I have to go back to the beginning and dump JSON?
我被困在这个问题上;有没有办法将这些对象转换为数组,还是我必须回到开头并转储 JSON?
回答by Paul Butcher
If I understand you correctly, there are two things you need to know to achieve this.
如果我对您的理解正确,那么您需要知道两件事才能实现这一目标。
One is the fact that object.member
can also be written as object["member"]
. This means that you do not have to hard-code your property names into your object to array translator, but can treat them as strings.
一是object.member
也可以写成object["member"]
. 这意味着您不必将属性名称硬编码到数组转换器的对象中,而是可以将它们视为字符串。
The second is the for-in statement (see section 12.6.4). For an object, this loops through all of its members.
第二个是 for-in 语句(参见第 12.6.4 节)。对于一个对象,这会循环遍历它的所有成员。
The following will result in innerArray being an array containing all of the values of all the members of the object
以下将导致innerArray成为一个包含对象所有成员的所有值的数组
var innerArray = [];
for (property in object) {
innerArray.push(object[property]);
}
Using this knowledge, you can tailor the array to contain whatever information it is you need to extract from the json objects.
使用这些知识,您可以调整数组以包含您需要从 json 对象中提取的任何信息。
回答by mkilmanas
There is a nice JavaScript library called Undersore.jswhich does all kind of object/array manipulations.
有一个很好的 JavaScript 库,叫做Undersore.js,它可以进行所有类型的对象/数组操作。
Using it you could do the conversion as easy as this
使用它,您可以像这样轻松地进行转换
_(json).each(function(elem, key){
json[key] = _(elem).values();
});
回答by Idealmind
There is is a simple way to do it
有一种简单的方法可以做到
With a unidimensional object:
对于一维对象:
var myarrayfromobject = new Array();
$H(myobject).each(function(item, i){
myarrayfromobject[i] = item;
});
If you have a multidimensional object, you can use te same idea, using a recursive function or a loop, verifying the type of item.
如果您有一个多维对象,您可以使用相同的想法,使用递归函数或循环,验证项目的类型。
回答by Gary Green
This will output:
这将输出:
var newJSON = [];
for (var i = 0, len = json.length; i < len; i++)
{
newJSON.push([json[i]]);
}
Into...
进入...
[
[Object { rating="0", title="The Killing Kind", more...}],
[Object { rating="0", title="The Third Secret", more...}]
]
Fiddle:http://jsfiddle.net/Ta6hW/
小提琴:http : //jsfiddle.net/Ta6hW/