简体   繁体   中英

Extract String property of an array of objects

I have several objects in an array which looks like this:

var objectArray = [
   abc: {id: "qq09qed0000", status: "running"}, 
   def: {id: "qq09qed0001", status: "paused"}, 
   ...
   xyz: {id: "qq09qed9999", status: "paused"}
];

EDIT: I am not sure, how it is represented in memory, the objectArray is created by doing array.push(object); several hundred times in a loop. The above is just an example to show you what the objects look like, they are truncated and in reality have lots more fields. Please stop telling me that the array is formatted wrong, it is, I am aware, it is just an example. The main thing to consider is how to go from an array of objects to an array of strings .

Edit2: Perhaps it looks like this in memory:

var objectArray = [
       {id: "qq09qed0000", status: "running"}, 
       {id: "qq09qed0001", status: "paused"}, 
       ...
       {id: "qq09qed9999", status: "paused"}
    ];

If I wanted an ordered array of just the ids (they are non sequential) of all of these elements, what would be the best way to extract them?

I have attempted a foreach loop,

for(var obj in objectArray ) {
    dataArray.push(objectArray[obj].getid());
}

But I am told this is not guarranteed to keep the ordering intact.

function sortById (a,b) 
  {
  if (a.id < b.id) return -1
  if (a.id > b.id) return 1;
  return 0;
  }

objectArray.sort (sortById);

Your objectArray is an object, not an array, and as such, has no obvious / defined ordering.

What order the elements are in, depends on the JavaScipt implementation of the browser it's running in.

For that reason, there is no way to "keep the ordering intact." , since there is no set ordering in the first place.

Now that you've edite the question, the objectArray isn't valid JavaScript syntax.

Arrays can't have key:value pairs like this:

var objectArray = [
    abc: {id: "qq09qed0000", status: "running"}, 

From what I gathered from your question, this should work:

data = data2.map(function(e){return e.id;});

This should return an array with only id values, in the same order as in data2 .

If you push all the ids in an array first, then sort them after, the fact that the 'for' function does not keep the ordering won't matter

Push all the ids in a new array

var objectArray = {
    abc: {id: "qq09qed0000", status: "running"}, 
    def: {id: "qq09qed0001", status: "paused"}, 
    ...
    xyz: {id: "qq09qed9999", status: "paused"}
};

var idArray = [];
for(var item in objectArray){

    idArray.push(item.id);
};

This will produce an array looking like this:

idsArray = ["qq09qed0000","qq09qed0001", ..., "qq09qed9999"]

Next, sort the array using the array sort method

idsArray.sort();

OR

idsArray.sort(function(a,b){
    return a - b;
});

This will sort them in alphanumeric order by default. You can also reverse the order using:

idsArray.sort(function(a,b){
    return b - a;
});
Namespace.pluck = function(obj, prop)
{
    var isFunction = Namespace.isFunction(prop);
    return Namespace.map(obj, function(key, element)
    {
        return isFunction ? prop(element) : element[prop];
    });
};

var ids = Namespace.pluck(objectArray, 'id').sort();

Array.prototype.sort() will give you a lexicographical sort, and not a natural sort. 10 will come after 1 but before 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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