immutable.js对象数组去重

immutable对象数组如何去重?

import { List } from 'immutable';
const arr1 = List([
    {name:'Jack'}, 
    {name:'Rose'}, 
    {name:'Albert'}, 
    {name:'Mike'}
]);
const arr2 = List([
    {name:'Jack'},
    {name:'Lawliet'}
]);

用什么方法可以将两个List合并,并去除掉值相同的对象,最终得到:

List([
    {name:'Jack'}, 
    {name:'Rose'}, 
    {name:'Albert'}, 
    {name:'Mike'},
    {name:'Lawliet'}
]);

还请大佬指点!

阅读 783
评论
    1 个回答
    • 2.8k
    const arr1 = [
      {name:'Jack', lev: 1}, 
      {name:'Rose'}, 
      {name:'Albert'}, 
      {name:'Mike'}
    ]
    
    const arr2 = [
      {name:'Jack', lev: 2},
      {name:'Lawliet'}
    ]
    
    function demo () {
      var res = [], a = 0, len;
      for(len = arguments.length; a < len; a++) {
        arguments[a].forEach(item => !res.some(x => x.name === item.name) && res.push(item))
      }
      return res
    }
    
    console.log(demo(arr1, arr2)); // => 完成
      撰写回答

      登录后参与交流、获取后续更新提醒