简体   繁体   English

Javascript:如何转换数组?

[英]Javascript: How can I transform an array?

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返回“string”,所以它是一个字符串。

[{"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

You can figure out if is a string or an already parsed object by checking the type of your variable, eg: 您可以通过检查变量的类型来确定是字符串还是已解析的对象,例如:

ajax('url', function (response) {
  alert(typeof response);
});

You will now figure out if it's a "string" or an Array "object" . 您现在将弄清楚它是"string"还是数组"object"

If it's a string, you can use the JSON.parse method as @alcuadrado suggest, otherwise you can simply use the array. 如果它是一个字符串,您可以使用JSON.parse方法作为@alcuadrado建议,否则您可以简单地使用该数组。

Several answers suggest the use of the for-in statement to iterate over the array elements, I would discourage you to use it for that. 几个答案建议使用for-in语句迭代数组元素,我不鼓励你使用它。

The for-in statement should be used to enumerate over object properties, to iterate over Arrays or Array-like objects, use a sequential loop as @Ken Redler suggests. for-in语句应该用于枚举对象属性, 迭代 Arrays或类似Array的对象,使用顺序循环,如@Ken Redler建议的那样。

You should really avoid for-in for 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.map method 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"]

This question is strongly related with this one . 这个问题是强烈相关的这一个

I would suggest reading my answer there, 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! Suerte!

Assuming your data always looks like that, you can do something like this: 假设您的数据总是如此,您可以执行以下操作:

var foo = [{"nomeDominio":"gggg.fa"},{"nomeDominio":"rarar.fa"}];
var newarr = [];
for ( var i=0,j=foo.length;i<j;i++ ) {
    newarr.push( foo[i]['nomeDominio'] );
}

Here's a working fiddle . 这是一个工作小提琴

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());

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 ? 你确定这是你想要的吗?

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'!): 好的,首先要获取“thing”的类型,使用“typeof”运算符(注意数组的类型是一个对象 ,而不是'array'!):

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: 要获得关联数组中的值(假设它是1),您可以循环遍历它,如下所示:

var d = [{"nomeDominio":"gggg.fa"},{"nomeDominio":"rarar.fa"}];
d["bob"] = "alice";
d["gary"] = "stephen";

for(var key in d) {
    alert(d[key]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM