gpt4 book ai didi

javascript - ImmutableJS : merge two list of objects, 不复制它们

转载 作者:行者123 更新时间:2023-11-29 10:33:56 24 4
gpt4 key购买 nike

假设我有以下内容:

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
}
]);

我想合并两个列表,同时删除重复项(在本例中为炸薯条),因此预期结果为:

{
'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
}

我正在尝试的(不删除欺骗):

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

返回合并的数组,但仍然重复。

我错过了什么?

最佳答案

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

关于javascript - ImmutableJS : merge two list of objects, 不复制它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39908169/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com