如何从两个不同的数组中获取一个数组

时间:2019-05-02 19:56:06

标签: javascript

我有两个数组:

let users = ["user1", "user2", "user3"];
let onlineUsers = [{name: "user1", id: 123}, {name: "user2, id: 345}];

如何从这些阵列中获得一个阵列。如果来自onlineUsers的用户存在于用户数组中以添加id,如果不存在,则id为null。像这样:

let result = [{name:"user1", id: 123}, {name:"user2", id: 345}, {name: "user3", id: null}]

0 个答案:

没有答案