Javascript:如何转换数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3610343/
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: How can I transform an array?
提问by MEM
I have this on a javascript var: (it's a http returned data, and I don't know if it's an array or string - (how can we see that?) - Update: using typeof returned "string", so it's a string.
我在 javascript var 上有这个:(它是一个 http 返回的数据,我不知道它是一个数组还是字符串 -(我们怎么能看到?) - 更新:使用 typeof 返回的“字符串”,所以它是一个字符串.
[{"nomeDominio":"gggg.fa"},{"nomeDominio":"rarar.fa"}]
How can we pass/transform that, into something like this:
我们如何将其传递/转换成这样的:
["gggg.fa","rarar.fa"]
?
?
Thanks a lot, MEM
非常感谢,MEM
采纳答案by alcuadrado
This question is strongly related with this one.
这个问题是强烈相关的这一个。
I would suggest reading my answerthere, as it would really help; and with a little variation, it would just work:
我建议在那里阅读我的答案,因为它真的很有帮助;稍加变化,它就可以工作:
var responseString = '[{"nomeDominio":"gggg.fa"},{"nomeDominio":"rarar.fa"}]',
responseObject = JSON.parse(responseString),
nombresDeDominio = [];
for(var i in responseObject) {
nombresDeDominio.push(responseObject[i].nomeDominio)
}
Suerte!
苏尔特!
回答by CMS
You can figure out if is a string or an already parsed object by checking the type of your variable, e.g.:
您可以通过检查变量的类型来确定是字符串还是已解析的对象,例如:
ajax('url', function (response) {
alert(typeof response);
});
You will now figure out if it's a "string"or an Array "object".
您现在将确定它是 a"string"还是 Array "object"。
If it's a string, you can use the JSON.parsemethod as @alcuadrado suggest, otherwise you can simply use the array.
如果它是一个字符串,你可以使用JSON.parse@alcuadrado 建议的方法,否则你可以简单地使用数组。
Several answers suggest the use of the for-instatement to iterateover the array elements, I would discourage you to use it for that.
几个答案建议使用该for-in语句来迭代数组元素,我不鼓励您为此使用它。
The for-instatement should be used to enumerateover object properties, to iterateover Arrays or Array-like objects, use a sequential loop as @Ken Redler suggests.
该for-in语句应该用于枚举对象属性,迭代数组或类似数组的对象,使用@Ken Redler 建议的顺序循环。
You should really avoid for-infor this purpose because:
你真的应该避免for-in这个目的,因为:
- The order of enumeration is not guaranteed, properties may not be visited in the numeric order.
- Enumerates also inherited properties.
- 不保证枚举的顺序,可能无法按数字顺序访问属性。
- 枚举还继承了属性。
You can also use the Array.prototype.mapmethod to meet your requirements:
您还可以使用该Array.prototype.map方法来满足您的要求:
var response = [{"nomeDominio":"gggg.fa"},{"nomeDominio":"rarar.fa"}];
var array = response.map(function (item) { return item.nomeDominio; });
// ["gggg.fa", "rarar.fa"]
回答by Ken Redler
回答by weekens
function transform(array, f) {
var ret = [];
$.each(array, function(index) {
var v = f.call(this, index);
if(v) {
ret.push(v);
}
});
return ret;
}
var result = transform(
[{"nomeDominio":"gggg.fa"},{"nomeDominio":"rarar.fa"}],
function() { return this.nomeDominio; }
);
alert(result.toString());
回答by Stephen
Okay, firstly to get the type of a "thing", use the "typeof" operator (note that the type of an array is an object, not 'array'!):
好的,首先要获取“事物”的类型,请使用“typeof”运算符(请注意,数组的类型是对象,而不是“数组”!):
var a = "string";
var b = 1;
var c = new Array();
alert(typeof(a)); // string
alert(typeof(b)); // number
alert(typeof(c)); // object
To get at the values in the associative array (assuming it is one), you can just loop through it, like so:
要获取关联数组中的值(假设它是一个),您可以循环遍历它,如下所示:
var d = [{"nomeDominio":"gggg.fa"},{"nomeDominio":"rarar.fa"}];
d["bob"] = "alice";
d["gary"] = "stephen";
for(var key in d) {
alert(d[key]);
}
回答by Guillaume Lebourgeois
it's a http returned data, and I don't know if it's an array or string
是http返回的数据,不知道是数组还是字符串
It's JSON, and you can use it directly in JavaScript.
它是JSON,您可以直接在 JavaScript 中使用它。
If you transform it into your array, you will lose the association key / value ; are you sure it's what you want ?
如果将其转换为数组,则会丢失关联键/值;你确定这是你想要的吗?

