I would use one Object and one Array, if you want to save some cycle:
var lookup = {};
var items = json.DATA;
var result = [];
for (var item, i = 0; item = items[i++];) {
var name = item.name;
if (!(name in lookup)) {
lookup[name] = 1;
result.push(name);
}
}
In this way you're basically avoiding the indexOf
/ inArray
call, and you will get an Array that can be iterate quicker than iterating object's properties – also because in the second case you need to check hasOwnProperty
.
Of course if you're fine with just an Object you can avoid the check and the result.push
at all, and in case get the array using Object.keys(lookup)
, but it won't be faster than that.