【问题标题】:Sorting an array of objects and creating new array with common values [duplicate]对对象数组进行排序并使用公共值创建新数组[重复]
【发布时间】:2020-02-28 01:29:06
【问题描述】:

我有以下对象数组:

let arr = [
    {
        status: 'Approved',
        domain: 'random.com',
        refID: '5005w1X4wL7:ref'
    },
    {
        status: 'Approved',
        domain: 'random123.com',
        refID: '5005w1X4wL7:ref'
    },
    {
        status: 'Approved',
        domain: 'helloworld.com',
        refID: '5005w1X4wL7'
    },
    {
        status: 'Approved',
        domain: 'helloworld123.com',
        refID: '5005w1X4wL7'
    }
]

抱歉,我是编码新手,但我需要对这个数组进行排序或过滤以找到匹配的 refID 并创建新的对象数组。请查看我正在寻找的输出示例:

let newArr = [
  {
    status: 'Approved',
    domain: ['random.com','random123.com'],
    refID: '5005w1X4wL7:ref'
  },
  {
    status: 'Approved',
    domain: ['hellworld.com','helloworld123.com'],
    refID: '5005w1X4wL7'
  }
]

【问题讨论】:

  • 欢迎来到 SO!到目前为止你做了什么来实现它。请张贴。
  • 我建议查看Array.prototype.reduceArray.prototype.forEach,或者考虑使用由refID 索引的Map
  • 请看how to ask a question。你总是想展示你尝试过的东西。很可能你真的很接近,你只需要一个小的调整。请记住,我们没有得到报酬,这是一个存档 - 不允许重复的问题。让我们轻松为您提供帮助。 :)
  • 您可以将thisgroupAndMerge(arr, 'refID', 'domain') 一起使用(第二个或第三个sn-p)

标签: javascript arrays loops sorting object


【解决方案1】:

我将创建一个stackDomainsByRef 函数,该函数使用.forEach 循环围绕arr 循环,并在将域添加到输出数组中的现有对象并设置后在内部进行常规循环以中断在 forEach 循环的每个步骤中从假到真的变量。如果变量保持为假,则将一个新对象添加到输出数组中。输出数组当然是返回的。

const arr = [
    {
        status: 'Approved',
        domain: 'random.com',
        refID: '5005w1X4wL7:ref'
    },
    {
        status: 'Approved',
        domain: 'random123.com',
        refID: '5005w1X4wL7:ref'
    },
    {
        status: 'Approved',
        domain: 'helloworld.com',
        refID: '5005w1X4wL7'
    },
    {
        status: 'Approved',
        domain: 'helloworld123.com',
        refID: '5005w1X4wL7'
    }
];
function stackDomainsByRef(array){
  const a = [];
  array.forEach(o=>{
    let w = false;
    for(let i=0,v,l=a.length; i<l; i++){
      v = a[i];
      if(o.refID === v.refID){
        v.domains.push(o.domain); w = true;
        break;
      }
    };
    if(w === false){
      a.push({status:o.status, domains:[o.domain], refID:o.refID});
    }
  });
  return a;
}
const newArr = stackDomainsByRef(arr);
console.log(newArr);

【讨论】:

    【解决方案2】:

    这是一种使用reduce的方法。

    const arr = [{
        status: 'Approved',
        domain: 'random.com',
        refID: '5005w1X4wL7:ref'
      },
      {
        status: 'Approved',
        domain: 'random123.com',
        refID: '5005w1X4wL7:ref'
      },
      {
        status: 'Approved',
        domain: 'helloworld.com',
        refID: '5005w1X4wL7'
      },
      {
        status: 'Approved',
        domain: 'helloworld123.com',
        refID: '5005w1X4wL7'
      }
    ];
    
    result = arr.reduce((acl, v, idx, self) => {
      acl[v['refID']] = acl[v['refID']] || v;
      var domain = acl[v['refID']]['domain'];
    
      if (domain.indexOf(v['domain']) < 0) {
        acl[v['refID']]['domain'] = domain + "," + v['domain'];
      }
    
      return acl;
    }, {});
    result = Object.values(result).map((v) => {
      v['domain'] = v.domain.split(",");
      return v;
    });
    
    console.log(result);

    【讨论】:

    • 您将domain 设为字符串。
    猜你喜欢
    • 2018-01-12
    • 2021-05-03
    • 1970-01-01
    • 2018-11-27
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    相关资源
    最近更新 更多