简体   繁体   English

提取对象数组的String属性

[英]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); 编辑:我不确定,它如何在内存中表示,objectArray是通过执行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: Edit2:也许在内存中看起来像这样:

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? 如果我只想要所有这些元素的ID的有序数组(它们是非顺序的),那么提取它们的最佳方法是什么?

I have attempted a foreach loop, 我尝试了foreach循环,

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. 您的objectArray是一个对象,而不是数组,因此没有明显的/定义的顺序。

What order the elements are in, depends on the JavaScipt implementation of the browser it's running in. 元素的顺序取决于运行它的浏览器的JavaScipt实现。

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. 既然您已经编辑了问题,那么objectArray不是有效的JavaScript语法。

Arrays can't have key:value pairs like this: 数组不能有如下的key:value对:

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 . 这应该返回一个只有id值的数组,其顺序与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 如果先将所有id推入数组中,然后再对它们进行排序,则'for'函数不能保持排序的事实无关紧要

Push all the ids in a new array 将所有ID推入新数组

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. Array.prototype.sort()将给您按字典顺序排序,而不是自然排序。 10 will come after 1 but before 2 10将在1之后但在2之前

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

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