在javascript数组对象中对项目进行分组 [英] Group items in javascript array object

查看:48
本文介绍了在javascript数组对象中对项目进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个javascript数组对象:

 可变项= [{address:'abc'},{address:'abc'},{address:'abc'},{address:'abc'},{address:'wer'},{address:'xyz'},{address:'xyz'},{address:'xyz'}]; 

我想做的是,我将2个项目视为一项..如果项目的地址重复两次..我将其视为一项..单个唯一项将被视为一项...

我想要这样的输出:

  [{address:'abc'},{address:'abc'},{address:'wer'},{address:'xyz'},{address:'xyz'}]; 

上面是预期的输出.

这个概念是这样的:

  abc,abc,abc应该是abc,abcxyz,xyz,xyz应该是xyz,xyz我们应该 

到目前为止,我尝试过的是:

 可变项= [{address:'abc'},{address:'abc'},{address:'abc'},{address:'abc'},{address:'wer'},{address:'xyz'},{address:'xyz'},{address:'xyz'}];var itemsNew = [];var preItem ='';var a = 1;var x = false;for(var i = 0; i< items.length; i ++){var itemDeliveryAddress = items [i] .address;if(a == 2){if(preItem == itemDeliveryAddress){preItem ='';a = 1;}别的 {preItem ='';a = 1;itemsNew.push(itemDeliveryAddress);}}别的 {a ++;preItem = itemDeliveryAddress;itemsNew.push(itemDeliveryAddress);}} 

但是上面的代码没有正确地对项目进行分组..我得到以下输出:

  abc,abc,wer,xyz,xyz 

注意:我已经按照升序对数组进行了排序

解决方案

您可以使用计数器和

I have a javascript array object:

var items = [
               {address: 'abc'}, {address: 'abc'}, {address: 'abc'}, 
               {address: 'abc'}, {address: 'wer'}, {address: 'xyz'}, 
               {address: 'xyz'}, {address: 'xyz'}
            ];

What I want to do is that I consider 2 items as one item.. If the address of items repeats twice.. i consider it as one item.. the single unique item will be treated as one...

I want its output like this:

[
   {address: 'abc'}, {address: 'abc'}, 
   {address: 'wer'}, 
   {address: 'xyz'}, {address: 'xyz'}
];

Above is the expected output..

The concept is like this:

abc , abc , abc SHOULD BE abc, abc
xyz , xyz, xyz SHOULD BE xyz, xyz
wer SHOULD BE wer

Well what I have tried so far is:

var items = [
           {address: 'abc'}, {address: 'abc'}, {address: 'abc'}, 
           {address: 'abc'}, {address: 'wer'}, {address: 'xyz'}, 
           {address: 'xyz'}, {address: 'xyz'}
        ];
  var itemsNew = [];
  var preItem  = '';
  var a        = 1;
  var x        = false;

  for(var i = 0; i < items.length; i++){

    var itemDeliveryAddress = items[i].address;

    if(a == 2){

      if(preItem == itemDeliveryAddress){

        preItem = '';
        a=1;
      }
      else {

        preItem = '';
        a=1;
        itemsNew.push(itemDeliveryAddress);
      }
    }
    else {

      a++;
      preItem = itemDeliveryAddress;
      itemsNew.push(itemDeliveryAddress);    
    }
  }

But the above code is not grouping the items properly.. I am getting this output:

abc,abc,wer,xyz,xyz

Note: I have sorted the array first in ascending order

解决方案

You could filter it with a counter and Array#filter.

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

This proposal uses the thisArg, a really empty object as this object inside of the callback.

Basically this[a.address] is the counter, which is if not exist, initialised with zero and then incremented by one.

After counting, the counter is used for the return. If the value is odd, the item is included in the result set.

While this is persistent in all callbacks, there is no need for sorting, because the counter need no specific order.

var items = [{ address: 'abc' }, { address: 'abc' }, { address: 'abc' }, { address: 'abc' }, { address: 'wer' }, { address: 'xyz' }, { address: 'xyz' }, { address: 'xyz' }],
    result = items.filter(function (a) {
        this[a.address] = (this[a.address] || 0) + 1;
        return this[a.address] % 2;
    }, Object.create(null));

console.log(result);

这篇关于在javascript数组对象中对项目进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆