简体   繁体   English

如何过滤对象数组?

[英]How to filter an array of objects?

I have an array of objects that have been pulled from a web service that look something like this. 我有一系列从Web服务中拉出的对象,看起来像这样。

{ 
  ArticleID: "ABC123", 
  Region: "US", 
  PubTypeID: "EO", 
  PubType: "Europe Order", 
  Created: "01/01/2013", 
  Scheduled: "03/02/2013"
} 

The ArticleID, Region & PubTypeID make up a composite key. ArticleID,Region和PubTypeID组成一个组合键。 The datasource is one table and cannot be changed but I need to get Unique items to display a dashboard like interface. 数据源是一个表,无法更改,但是我需要获取“唯一”项以显示类似界面的仪表板。 So Show Unique ArticleID's but group the articles under them. 因此,请显示“唯一的ArticleID”,但将其下的文章分组。 The interface should look something like this. 该界面应如下所示。

ArticleID      Status
----------------------
ABC123          Scheduled
    Region:US       Scheduled: 02/02/2013
    Region:JP       Scheduled: 05/02/2013
    Region:OE       Scheduled: 03/02/2013
    Region:EU       Scheduled: 04/02/2013
ABC456          Ready to Schedule          
    Region:US       
    Region:US       
    Region:US       
ABC789          Sent
    Region:EU       Sent On: 01/02/2013
    Region:EU       Sent On: 01/02/2013
    Region:EU       Sent On: 01/02/2013
    Region:EU       Sent On: 01/02/2013

The status is determined by the date. 状态由日期确定。 I also have the dilema of what the status will be if one item under an article has a date and one does not, what will the status be? 如果文章下的一项有日期而没有日期,那么我的状态也将是个难题。状态是什么?

If the data was normalized I would have no issues here as I could do multiple selects and build the output but I only have a flat result set so need to do the "selecting" in Javascript. 如果数据被规范化,那么这里将没有问题,因为我可以进行多次选择并构建输出,但是我的结果集很平坦,因此需要在JavaScript中进行“选择”。

Can someone help me on how I would do this in Javascript please? 有人可以帮我如何用Java语言执行此操作吗?

In order to build this data out to go into a table as you describe, I would first organize the data so it matches what you want to do. 为了将这些数据扩展到您所描述的表中,我将首先对数据进行组织,使其与您要执行的操作匹配。 So instead of having individual objects, I would iterate through the returned data and convert it into: 因此,我将不遍历返回的数据并将其转换为:

{ArticleID: "ABC123", 
Status: "Scheduled", 
Articles: { 
  ArticleID: "ABC123", 
  Region: US, 
  PubTypeID: "EO", 
  PubType: "Europe Order", 
  Created: "01/01/2013", 
  Scheduled: "03/02/2013"
  },{ 
  ArticleID: "ABC234", 
  Region: US, 
  PubTypeID: "EO", 
  PubType: "Europe Order", 
  Created: "01/01/2013", 
  Scheduled: "03/02/2013"
  }
}

Then build the table based on this structure. 然后基于此结构构建表。 You will need to have some calculations in your function to determine the status. 您需要在函数中进行一些计算以确定状态。 I don't understand this one enough to give you a specific answer, but it looks like all common article ID's have the same status, so this could be ignored while restructuring the objects, and inserted while building the table. 我对这一问题的理解还不够,无法为您提供具体的答案,但是看起来所有常见的文章ID的状态都相同,因此在重组对象时可以将其忽略,在构建表时将其插入。

If you have any control over the server side code, it would be much better to do this object restructure work before passing it to the client. 如果您对服务器端代码有任何控制权,那么在将此对象重组工作传递给客户端之前,最好做一下。

You could use Array.prototype.filter , to sort all entries, given the fact you know all the article IDs (If not, you would have to iterate over the input and collect them). 考虑到您知道所有文章ID的事实,您可以使用Array.prototype.filter对所有条目进行排序(如果不知道,则必须遍历输入并收集它们)。

var input = [{ 
  ArticleID: "ABC123", 
  Region: "US"
},{ 
  ArticleID: "ABC456", 
  Region: "US"
}];

var regions = ["ABC123","ABC456"];

var articles = [];
regions.forEach(
    function(reg,i){
        articles[i] = input.filter( function(el){ return el.ArticleID == reg} );
    }
);

console.log(regions);
//regions[0] <= array with all articles with regionID "ABC123"
//regions[1] <=                   - " -               "ABC456"

now articles holds an array with all articles sorted by ArticleID. 现在, articles包含一个数组,其中所有文章均按ArticleID排序。

Beware, this is IE9+ code, in order to make it work in IE8 and below, you need a shim for filter and forEach which you can find on the according MDN-Articles. 请注意,这是IE9 +代码,为了使其能够在IE8及以下版本中工作,您需要一个垫片用于filterforEach ,您可以在相应的MDN-Articles上找到它。

I think that you need to re-arrange the data having it ordered by ArticleID. 我认为您需要重新安排按ArticleID排序的数据。 And assuming you are going to have many articles. 并假设您将要撰写许多文章。 Something like this iteration could do the re-arranging task. 像这样的迭代可以完成重新排列任务。 Then you can easily iterate trough the new object printing the useful information 然后,您可以轻松地遍历新对象,打印出有用的信息

var x
var y = 0;
var newArray = new Array();
for(key in data){
   x = data[key].articleID; //The value of the key you want to use (ArticleID)
   newArray[x] = newArray[x] || []
   newArray[x][y] = data[key];
   y++
}
console.log(newArray)

Here you have a fiddle. 在这里,你有一个小提琴。 Look into the resulting object (using the chrome or other console), and you are going to see how the data is now grouped by ArticleID; 查看结果对象(使用chrome或其他控制台),您将看到现在如何按ArticleID对数据进行分组;

EDIT: I've replaced the code with a cleaner version. 编辑:我已将代码替换为更干净的版本。

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

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