javascript数组按一个属性的映射

时间:2019-02-19 15:05:04

标签: javascript arrays

我有两个数组

Array(4)
0: {Username: "admin", Enabled: true, ...}
1: {Username: "ftyjfj", Enabled: true, ...}
2: {Username: "ghjfgj", Enabled: true, ...}
3: {Username: "foo", Enabled: true, ...}


Array(4)
0: User {username: "foo", email: "foo@foo.com", enabled: true, status: "CONFIRMED", isAdmin: false}
1: User {username: "admin", email: "admin@foo.com", enabled: true, status: "CONFIRMED", isAdmin: false}
2: User {username: "sgrillon", email: "sgrillon@foo.com", enabled: true, status: "CONFIRMED", isAdmin: false}
3: User {username: "toto", email: "toto@foo.com", enabled: true, status: "CONFIRMED", isAdmin: false}

我要从第二个数组更改isAdmin属性,如果 username存在于第一个数组中(在Username属性上)。

因此:isAdmin: true用于foo和admin:

Array(4)
0: User {username: "foo", email: "foo@foo.com", enabled: true, status: "CONFIRMED", isAdmin: true}
1: User {username: "admin", email: "admin@foo.com", enabled: true, status: "CONFIRMED", isAdmin: true}
2: User {username: "sgrillon", email: "sgrillon@foo.com", enabled: true, status: "CONFIRMED", isAdmin: false}
3: User {username: "toto", email: "toto@foo.com", enabled: true, status: "CONFIRMED", isAdmin: false}

4 个答案:

答案 0 :(得分:1)

您可以使用map + some的组合。这是一个解决方案:

const next = users.map(user =>
  Object.assign({}, user, {
    isAdmin: data.some(d => d.Username === user.username),
  })
);

答案 1 :(得分:1)

我使用.forEach.some进行了实时演示,请参见下面的代码:

var a = [{
  Username: "admin"
}, {
  Username: "ftyjfj"
}, {
  Username: "ghjfgj"
}, {
  Username: "foo"
}]
var b = [{
  username: "foo",
  email: "foo@foo.com",
  enabled: true,
  status: "CONFIRMED",
  isAdmin: false
}, {
  username: "admin",
  email: "admin@foo.com",
  enabled: true,
  status: "CONFIRMED",
  isAdmin: false
}, {
  username: "sgrillon",
  email: "sgrillon@foo.com",
  enabled: true,
  status: "CONFIRMED",
  isAdmin: false
}, {
  username: "toto",
  email: "toto@foo.com",
  enabled: true,
  status: "CONFIRMED",
  isAdmin: false
}]

b.forEach(obj => {
  if (a.some(val => val.Username === obj.username)) {
    obj.isAdmin = true;
  }
  return obj;
})

console.log(b)

答案 2 :(得分:0)

首先为了简化检查第一个数组中的存在,让我们将其设为地图:

let mappedUsers = firstArray.reduce((m, el) => { m[el.username] = el; return m; }, {});

然后,我们只需检查用户名是否在地图中作为键存在,即可轻松更新第二个数组:

secondArray.map(el => { if (el.username in mappedUsers) { el.isAdmin = true; } return el; });

或者我们可以使用forEach遍历每个元素并相应地更新它:

secondArray.forEach(el => { if (el.username in mappedUsers) { el.isAdmin = true; } });

答案 3 :(得分:0)

解决此问题的一种方法是,首先从第一个数组的usernames创建一个Set。然后,您可以循环遍历第二个数组,如果在提到的username上存在Set,我们将isAdmin属性更改为true

var arr1 = [
  {username: "admin", enabled: true},
  {username: "ftyjfj", enabled: true},
  {username: "ghjfgj", enabled: true},
  {username: "foo", enabled: true}
];

var arr2 = [
  {username: "foo", email: "foo@foo.com", enabled: true, status: "CONFIRMED", isAdmin: false},
  {username: "admin", email: "admin@foo.com", enabled: true, status: "CONFIRMED", isAdmin: false},
  {username: "sgrillon", email: "sgrillon@foo.com", enabled: true, status: "CONFIRMED", isAdmin: false},
  {username: "toto", email: "toto@foo.com", enabled: true, status: "CONFIRMED", isAdmin: false}
];

let adminSet = new Set(arr1.map(o => o.username));
arr2.forEach(x => adminSet.has(x.username) && (x.isAdmin = true));
console.log(arr2);