简体   繁体   English

过滤出二维数组的值

[英]Filtering out values of a bidimensional array

I have a bidimensional array which looks like this: 我有一个像这样的二维数组:

[[10, 20, 30, S1]
[10, 20, 30, S1]
[10, 20, 30, S1]
[50, 70, 80, G1]
[50, 70, 80, G1]
[50, 70, 80, G1]]

I want to filter out every "non-first" occurence of the 4th value of each sub-array, like this: 我想过滤出每个子数组第4个值的每个“非第一个”出现,如下所示:

[[10, 20, 30, S1]
[10, 20, 30]
[10, 20, 30]
[50, 70, 80, G1]
[50, 70, 80]
[50, 70, 80]]

How is this possible? 这怎么可能? Thanks in advance 提前致谢

You can use an object to keep track of the found values, and remove the last item from the subarrays when it was found before: 您可以使用一个对象来跟踪找到的值,并在之前找到子数组时从子数组中删除它:

var items = {};
for (var i = 0; i < arr.length; i++) {
  var id = arr[i][arr[i].length - 1];
  if (items.hasOwnProperty(id)) {
    arr[i].pop();
  } else {
    items[id] = 1;
  }
}

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

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