javascript通过映射更新基于另一个对象的对象键/值

时间:2018-01-07 12:24:43

标签: javascript arrays object for-loop ecmascript-6

我正在循环某个对象并根据某些查找更新另一个对象的值,目前我在映射中获取正确的值时遇到问题。

let users = [
  {
    "id": "123",
    "email": "john.doe@acme.com",
    "name": " John Doe",
    "group": "1",
    "subgroup": "1"
  },
  {
    "id": "456",
    "email": "tom.hans@stk.com",
    "name": " Tom hans",
    "group": "2",
    "subgroup": "1"
  }
];

let groupData = [
  {
    id: "1",
    name: "Stars",
    subgroup: [
        {
          id: "1",
          pseudo: "big"
        },
        {
          id: "2",
          pseudo: "small"
        }
      ]
  },
  {
    id: "2",
    name: "Stones",
    subgroup: [
        {
          id: "1",
          pseudo: "tiny"
        },
        {
          id: "2",
          pseudo: "huge"
        }
      ]
  }
];

const allUsers = {};
for (var i = 0; i < users.length; i++) {
    let groupID = users[i].group,
        subgroupID = users[i].subgroup;

    users[i].group = users[i].group ? groupData[groupID][name] : '';
    users[i].subgroup = users[i].subgroup ?
        groupData[groupID].subgroup[subgroupID][name] : '';

    allUsers[users[i].id] = users[i];
}

allUsers的结果如下:

{
  123: {
    "id": "123",
    "email": "john.doe@acme.com",
    "name": " John Doe",
    "group": "Stars",
    "subgroup": "big"
  },
  456: {
    "id": "456",
    "email": "tom.hans@stk.com",
    "name": " Tom hans",
    "group": "Stones",
    "subgroup": "tiny"
  }
};

目前,我在for loop内部有查找问题,以获取组/子组的名称。

有人可以帮忙解决这个问题吗?最好用ecma6。

2 个答案:

答案 0 :(得分:1)

您正在使用groupData,就好像它是groupID的键。但是,它是一个数组。你应该尝试使用它的find方法:

const allUsers = {};
for (var i = 0; i < users.length; i++) {
    let groupID = users[i].group,
        subgroupID = users[i].subgroup;
    let userGroup = groupData.find(group => group.id === groupID);

    users[i].group = userGroup.name
    users[i].subgroup = userGroup.subgroup.find(subgroup => subgroup.id === subgroupID).pseudo;

    allUsers[users[i].id] = users[i];
}

您正在修改原始数据,这不是一个好主意。 您可以在循环中创建一个新用户,并使用Object.assign来避免这种情况。

以下是我只使用reduce方法的方法:

const allUsers = users.reduce((data, user) => {
    const group = groupData.find(group => group.id === user.group);
    const name = group ? group.name : '';
    const subGroup = group ? group.subgroup.find(subgroup => subgroup.id === user.subgroup): null;
    const pseudo = subGroup ? subGroup.pseudo : '';
    data[user.group] = Object.assign({}, user, { group: name, subGroup: pseudo });
   return data;
}, {});

Object.assign创建一个新对象,因此不会改变现有用户。

答案 1 :(得分:1)

您可以使用array#reduce累积您的用户,并使用array#find获取group名称和subgroup pseudo名称。

&#13;
&#13;
let users = [ { "id": "123", "email": "john.doe@acme.com", "name": " John Doe", "group": "1", "subgroup": "1" }, { "id": "456", "email": "tom.hans@stk.com", "name": " Tom hans", "group": "2", "subgroup": "1" } ],
    groupData = [ { id: "1", name: "Stars",subgroup: [ { id: "1", pseudo: "big" }, { id: "2", pseudo: "small" } ] }, { id: "2", name: "Stones", subgroup: [ { id: "1", pseudo: "tiny" }, { id: "2", pseudo: "huge" } ] } ],
    result = users.reduce((r, user) => {
      var {name = '', subgroup = []} = groupData.find(o => o.id === user.group); 
      r[user.id] = Object.assign({}, user, {group: name}, {subgroup: subgroup.find(o => o.id === user.subgroup).pseudo||''});
      return r;
    }, {});
    
console.log(result);
&#13;
&#13;
&#13;