ImmutableJS:合并两个对象列表,而不复制它们 [英] ImmutableJS: merge two list of objects, without duplicating them

查看:375
本文介绍了ImmutableJS:合并两个对象列表,而不复制它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下内容:

var allFoods = Immutable.List();

var frenchFood = Immutable.List([
  {
  'type': 'french fries',
  'price': 3
  },
  {
    'type': 'petit gateau',
    'price': 40
  },
  {
    'type': 'croissant',
    'price': 20
  },
]);

var fastFood = Immutable.List([
  {
  'type': 'cheeseburger',
  'price': 5
  },
  {
  'type': 'vegan burger',
  'price': 20
  },
  {
  'type': 'french fries',
  'price': 3
  }
]);

我想合并两个列表,同时删除偶像(在这种情况下为炸薯条),因此预期结果将是:

I want to merge both lists, in a way that I also remove dupes (in this case, french fries), so the expected result would be:

{
'type': 'french fries', // keep the first french fries
'price': 3
},
{
  'type': 'petit gateau',
  'price': 40
},
{
  'type': 'croissant',
  'price': 20
},
  {
'type': 'cheeseburger',
'price': 5
},
{
'type': 'vegan burger',
'price': 20
}

我正在尝试(不删除重复项):

What I'm trying (doesn't remove dupes):

allFoods = frenchFood.concat(fastFood);
allFoods = allFoods.filter(function(item, pos) {
    return allFoods.indexOf(item) === pos;
});

返回合并但仍重复的数组.

Which returns arrays merged, but still duplicated.

我想念什么?

推荐答案

const allFoods = frenchFood.concat(fastFood.filter((item) =>
   frenchFood.indexOf(item) < 0
));

这篇关于ImmutableJS:合并两个对象列表,而不复制它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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