搜索数组中的对象并更新是否存在或添加

时间:2019-12-28 16:52:26

标签: javascript ecmascript-6

这是我登录的用户列表的样子:

const connectedUsers = [{
  user: {
    uuid: 'b62-2dw',
    points: 1,
  },
  id: "1234567"
}];

当新用户登录到我的系统时,我将连接此表:

this.connectedUsers = [
  ...this.connectedUsers,
  {
    ...payload,
    id: client.id
  },
];

然后我的数组如下所示:(我对此有个更好的理解)

const connectedUsers = [{
    user: {
      uuid: 'b62-2dw',
      points: 1,
    },
    id: "1234567"
  },
  {
    user: {
      uuid: '663-dda',
      points: 5,
    },
    id: "33332"
  }
];

如果用户使用663-dda这样的uuid更新了他的观点,我将再次执行此方法。 当我保持原样时,将执行以下操作:

const connectedUsers = [{
    user: {
      uuid: 'b62-2dw',
      points: 1,
    },
    id: "1234567"
  },
  {
    user: {
      uuid: '663-dda',
      points: 5,
    },
    id: "33332"
  },
  {
    user: {
      uuid: '663-dda',
      points: 6,
    },
    id: "33332"
  }
];

我想编写一个非常好的算法(使用ES6 +),该算法首先检查该数组中是否存在这样的对象(通过iduser.uuid检查)。如果是这样,请更新。如果没有,请添加一个新对象。所以应该像这样:

const connectedUsers = [{
    user: {
      uuid: 'b62-2dw',
      points: 1,
    },
    id: "1234567"
  },
  {
    user: {
      uuid: '663-dda',
      points: 6,
    },
    id: "33332"
  }
];

3 个答案:

答案 0 :(得分:0)

在用户登录时更新数组的代码中,您可以这样做:

if (!this.connectedUsers.find(user => user.user.uuid === payload.user.uuid) {
  this.connectedUsers = [
    ...this.connectedUsers,
    { ...payload, id: client.id },
  ];
}

答案 1 :(得分:0)

const connectedUsers = [{
            user: {
                uuid: 'b62-2dw',
                points: 1,
            },
            id: "1234567"
        },
        {
            user: {
                uuid: '663-dda',
                points: 5,
            },
            id: "33332"
        },
        {
            user: {
                uuid: '663-dda',
                points: 6,
            },
            id: "33332"
        }];
//let take some sample object

const newObDuplicateUuid = {
            user: {
                uuid: '663-dda',
                points: 6,
            },
            id: "3333290"
        }
        
const newObDuplicateId = {
            user: {
                uuid: '756-dda',
                points: 6,
            },
            id: "33332"
        }
        
const newObFresh = {
            user: {
                uuid: '756-dda',
                points: 6,
            },
            id: "3333290"
        }

let checkRule = connectedUsers.every(item => item.user.uuid != newObDuplicateUuid.user.uuid && item.id != newObDuplicateUuid.id)

//return false for same uuid

checkRule = connectedUsers.every(item => item.user.uuid != newObDuplicateId.user.uuid && item.id != newObDuplicateId.id)

//return false for same id

checkRule = connectedUsers.every(item => item.user.uuid != newObFresh.user.uuid && item.id != newObFresh.id)

//return true
console.log('Passed validation :'+checkRule);

const result = checkRule ? [...connectedUsers,newObFresh] : 'There is duplicate value';

console.log(result);

答案 2 :(得分:0)

这是我的方法。一种功能涵盖了两种情况。只需传递arraynew entry,它将返回updated list

function updateOrAddUser(listOfUsers, newEntry) {
    let found = false;

    const updatedUserList = listOfUsers.map(entry => {
        if (entry.user.uuid === newEntry.user.uuid) {
            found = true;

            return newEntry;
        }

        return entry;
    });

    if (!found) {
         updatedUserList.push(newUser);
    }

    return updatedUserList;
}