【问题标题】:Filter array based on attributes in javascript/ react基于 javascript/react 中的属性过滤数组
【发布时间】:2017-11-22 22:41:40
【问题描述】:

您好,我需要过滤具有相同地址的公司(数组)并创建一个数组,例如:

 [{address:uniqueaddress1,organization:[company1,company2]},
  {address:uniqueaddress2,organization:[company3,company4]
  .....]   

我正在使用以下代码:

 var organizations = [];
 var  dataPoints = [];
 for (var i = 0; i < companies.length; i++) {
  for (var j = 0; j < companies.length; j++) {
     if (i === j) continue;
     if (companies[j].address === companies[i].address) {
        organizations.push(companies[j]);            
        companies[j].added = true;  //To mark it is added
    }
    dataPoints.push({address:companies[j].address, organizations: companies[j]});
   }   
 }

原始数组:

  0:Object
  added:true
  address:"sample address 1"
  id:258
  latitude:90.90227
  longitude:12.538208
  name:"Company name 1"
  postalCode:"90450"

【问题讨论】:

  • 原始数组是什么?
  • @Ted 编辑问题
  • 等一下!这不是 uber 的 CodeFights 机器人吗?哈哈

标签: javascript arrays reactjs for-loop javascript-objects


【解决方案1】:

您可以使用Map 以更好的方式实现此目的(尽管您也可以将这种方法与数组一起使用,就像您已经完成的那样)。您还可以删除内部循环,以便在更大的结果集上稍微提高效率,方法是在进行时添加,而不是为每个地址再次循环。

let original = [
    { address: '123 Example Street', id: 1 }, 
    { address: '123 Example Street', id: 2 },
    { address: '456 Example Street', id: 3 }
];

let grouped = new Map();

original.forEach(function(company) {

    let companies = grouped.get(company.address);

    // If we already have the key, then just push into the array.
    if (companies !== undefined) {
        companies.push(company);
    }
    else {
        // ...if not then create a new array.
        companies = [company];
    }

    grouped.set(company.address, companies);

});

【讨论】:

  • 感谢您的回复。由于我正在使用 React 项目,请您指导如何将公司推向数据点?
  • 在这种情况下使用 React 应该没有什么不同,您在这里只处理原生 JavaScript 的东西。此示例中的 grouped Map 将包含与您放入 dataPoints 数组中的数据相同的数据。
【解决方案2】:

如果您习惯使用ES6 语法,那么您可以使用filtermap 方法来做到这一点。下面的代码过滤了数组comapanies,map方法创建了一个临时数组,然后我们使用indexOf方法检查我们的地图中是否可以有相同的对象。

let companies = [{
  "added": true,
  "address": "sample address 1",
  "id": 258,
  "latitude": 90.90227,
  "longitude": 12.538208,
  "name": "Company name 1"
}, {
  "added": true,
  "address": "sample address 1",
  "id": 258,
  "latitude": 90.90227,
  "longitude": 12.538208,
  "name": "Company name 1"
}, {
  "added": true,
  "address": "sample address 2",
  "id": 258,
  "latitude": 90.90227,
  "longitude": 12.538208,
  "name": "Company name 1"
}, {
  "added": true,
  "address": "sample address 2",
  "id": 258,
  "latitude": 90.90227,
  "longitude": 12.538208,
  "name": "Company name 1"
}]

function uniqueArray(array, prop) {
  return array.filter((obj, pos, arr) => {
    return arr.map(mapObj => mapObj[prop]).indexOf(obj[prop]) === pos;
  });
}
console.log(uniqueArray(companies, "address"))

Credits

【讨论】:

  • 虽然我认为这将是 O(N²),对吧? filter 将遍历所有项目,map?
  • @TomDavies yes filtermap 都将遍历所有项目
猜你喜欢
  • 1970-01-01
  • 2021-10-08
  • 2019-10-14
  • 1970-01-01
  • 2021-03-13
  • 2018-08-23
  • 2018-08-17
  • 1970-01-01
  • 2018-10-11
相关资源
最近更新 更多