You can determine if a string or an object has already been processed by checking the type of your variable, for example:
ajax('url', function (response) { alert(typeof response); });
Now you will find out if it is a "string" or an array of "object" .
If it is a string, you can use the JSON.parse method, as @alcuadrado suggests, otherwise you can just use an array.
Several answers suggest using the for-in statement to iterate over the elements of an array, I would discourage you from using it for this.
The for-in operator should be used to list the properties of an object, to iterate over arrays or objects like Array, use a sequential loop, as @Ken Redler suggests.
You really should avoid for-in for this purpose, because:
- The order of listing is not guaranteed; properties cannot be visited in numerical order.
- Enumerates also inherited properties.
You can also use the Array.prototype.map method to suit your requirements:
var response = [{"nomeDominio":"gggg.fa"},{"nomeDominio":"rarar.fa"}]; var array = response.map(function (item) { return item.nomeDominio; });
CMS
source share