使用JS过滤和搜索2个不同的对象数组

时间:2017-05-09 20:33:46

标签: javascript arrays

我有两个对象数组,我需要弄清楚用另一个数组的对象过滤其中一个数组的最佳方法是什么?

第一个数组称为drinksType

[
  {
    "id": "GROUP1",
    "type": "Water"
  },
  {
    "id": "GROUP2",
    "type": "Beer"
  },
  {
    "id": "GROUP3",
    "type": "Coke"
  },
  {
    "id": "GROUP4",
    "type": "Gin"
  }
]

第二个名为drinksGroup,会针对drinksType

进行过滤
[
  {
    "GROUP1": 1,
    "GROUP2": 0,
    "GROUP3": 0,
    "GROUP4": 0
  },
  {
    "GROUP1": 1,
    "GROUP2": 1,
    "GROUP3": 0,
    "GROUP4": 0
  },
  {
    "GROUP1": 0,
    "GROUP2": 0,
    "GROUP3": 1,
    "GROUP4": 0
  },
  {
    "GROUP1": 0,
    "GROUP2": 1,
    "GROUP3": 1,
    "GROUP4": 1
  }
]

所需的结果将是这样的

[
  {
    "name": "Water"
  },
  {
    "name": "Water Beer"
  },
  {
    "name": "Coke"
  },
  {
    "name": "Beer Coke Gin"
  }
]

因此,您可以看到drinksGroup中的某个键的值是否为1,那么我需要过滤drinksType来获取该类型。在某些情况下,可能有几个键的值为1,在这种情况下,我需要使用所有键过滤drinksType并加入类型。感叹!

到目前为止,我的努力就是这个......

const dataKeys = Object.keys(drinksGroup);

drinks.name = [...dataKeys
            .filter((dataKey) => dataKey.match(/GROUP/))
            .filter((dataKey) => data[dataKey] === 1)
        ].join(' ');

这给出了结果

  [
      {
        "name": "GROUP1"
      },
      {
        "name": "GROUP1 GROUP2"
      },
      {
        "name": "GROUP3"
      },
      {
        "name": "GROUP2 GROUP3 GROUP3"
      }
    ]

但我不知道该怎么做。我有什么想法可以做到吗?我工作的项目也可以访问lodash,如果这更容易?非常感谢

5 个答案:

答案 0 :(得分:2)



var drinksType = [{"id":"GROUP1","type":"Water"},{"id":"GROUP2","type":"Beer"},{"id":"GROUP3","type":"Coke"},{"id":"GROUP4","type":"Gin"}];
var drinksGroup = [{"GROUP1":1,"GROUP2":0,"GROUP3":0,"GROUP4":0},{"GROUP1":1,"GROUP2":1,"GROUP3":0,"GROUP4":0},{"GROUP1":0,"GROUP2":0,"GROUP3":1,"GROUP4":0},{"GROUP1":0,"GROUP2":1,"GROUP3":1,"GROUP4":1}];

var drinksNames = drinksGroup.map((group) => {
    var name = '';
    for (var groupName in group) {
        if (group[groupName]) {
            name += drinksType.find((type) => type.id === groupName).type + ' ';
        }
    }
    return {name: name.slice(0, -1)};
});

console.log(drinksNames);




那会有用吗?

请注意,如果您要定位IE,则Array.prototype.find不受支持。

答案 1 :(得分:2)

其他可能的解决方案。

Visit.joins(:post).group([:post_id, :title]).select('sum(cpc_bid) as earnings', :post_id, :title, 'count(visits.id) as total_views').where(influencer: current_user)

答案 2 :(得分:0)

地图,过滤器:

const drinksType = [{"id":"GROUP1","type":"Water"},{"id":"GROUP2","type":"Beer"},{"id":"GROUP3","type":"Coke"},{"id":"GROUP4","type":"Gin"}],
    drinksGroup = [{"GROUP1":1,"GROUP2":0,"GROUP3":0,"GROUP4":0},{"GROUP1":1,"GROUP2":1,"GROUP3":0,"GROUP4":0},{"GROUP1":0,"GROUP2":0,"GROUP3":1,"GROUP4":0},{"GROUP1":0,"GROUP2":1,"GROUP3":1,"GROUP4":1}];

const r = drinksGroup.map(e => Object.keys(e).filter(k => e[k] === 1))
  .map(a => Object.assign({}, 
    {name: a.map(g => drinksType.filter(({id}) => id === g).pop().type).join(' ')}
  ));
 
console.log(JSON.stringify(r, null, 2));

首先我们找到值为1的组,然后过滤类型并创建字符串。

答案 3 :(得分:0)

为类型创建drinksType ID地图,然后使用drinksGrouptypeByGroupIdMap中映射每个对象:



const drinkTypes = [{"id":"GROUP1","type":"Water"},{"id":"GROUP2","type":"Beer"},{"id":"GROUP3","type":"Coke"},{"id":"GROUP4","type":"Gin"}];

const drinkGroups = [{"GROUP1":1,"GROUP2":0,"GROUP3":0,"GROUP4":0},{"GROUP1":1,"GROUP2":1,"GROUP3":0,"GROUP4":0},{"GROUP1":0,"GROUP2":0,"GROUP3":1,"GROUP4":0},{"GROUP1":0,"GROUP2":1,"GROUP3":1,"GROUP4":1}];

const typeByGroupIdMap = drinkTypes.reduce((map, { id, type }) => map.set(id, type), new Map);

const result = drinkGroups.map((groups) => ({
  name: Object.keys(groups).reduce((arr, group) => {
    groups[group] && arr.push(typeByGroupIdMap.get(group));
    return arr;
  }, []).join(' ')
}));

console.log(result);




答案 4 :(得分:0)

您可以通过仅过滤真实的属性来使用自上而下的方法。它需要一个包含所有组及其相关类型的对象。



var drinksType = [{ id: "GROUP1", type: "Water" }, { id: "GROUP2", type: "Beer" }, { id: "GROUP3", type: "Coke" }, { id: "GROUP4", type: "Gin" }],
    drinksGroup = [{ GROUP1: 1, GROUP2: 0, GROUP3: 0, GROUP4: 0 }, { GROUP1: 1, GROUP2: 1, GROUP3: 0, GROUP4: 0 }, { GROUP1: 0, GROUP2: 0, GROUP3: 1, GROUP4: 0 }, { GROUP1: 0, GROUP2: 1, GROUP3: 1, GROUP4: 1 }],
    groups = Object.assign(...drinksType.map(o => ({[o.id]: o.type }))),
    result = drinksGroup
        .map(a => Object
            .keys(a)
            .sort()
            .filter(k => a[k])
            .map(k => groups[k])
            .join(' ')
        );

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }