javascript - 使用数组中的值更新对象数据

标签 javascript arrays object

我正在尝试使用 individualData 数组的“positionTitle”字段更新 graphData 对象的“positionTitle”字段。

我需要准确地做到这一点,两组数据都有“帐户”,它们都有相同的用户 ID 和全名,我希望尝试使用它来进行匹配。

我希望具有相同帐户 ID 或名称(以更容易的为准)的用户的positionTitle 进入对象字段。

这是我目前所拥有的: 我的对象(我想更新):

graphData = {

"name": "Annual meetings",
  "engagementAreas": [{
    "id": "1",
    "engagementTypes": [{
        "name": "forestry",
        "engagements": []
      },
      {
        "name": "houses",
        "engagements": [{
          "name": "engagement1",
          "members": [{
              "id": "e334", "account": {
                  "id": "eefe", "fullName": "jim bean"
              },
            "position": {
              "id": "3434",
              "positionTitle": "Manager"
            }
          }]
        }]
      },
 {
  "name": "landscaping",
  "engagements": [{
    "name": "engagement1343",
    "members": [{
        "id": "e334", "account": {
            "id": "123", "fullName": "john boer"
        },
      "position": {
        "id": "4545",
        "positionTitle": "Senior Manager"

      }
      }]
     }]
    }
   ]
},

{
"name": "community days",
    "engagementTypes": [{
        "name": "skyscraping",
        "engagements": []
      },
      {
        "name": "tennis",
        "engagements": [{
          "name": "engagement346",
          "members": [{
              "id": "34", "account": {
                  "id": "0010X000048DDMsQAO", "fullName": "edy long"
              },
            "position": {
              "id": "3999434",
              "positionTitle": "Ultime Manager"
            }
          }]
        }]
      },
 {
  "name": "Juicing",
  "engagements": [{
    "name": "347343",
    "members": [{
        "id": "4546", "account": {
            "id": "001b000003WnPy1AAF", "fullName": "jeff bint"
        },
      "position": {
        "id": "35006",
        "positionTitle": "Senior Ultimate Manager"

      }
    }]
}]
 }]
}]
}

我想要获取其positionTitles的数组:

IndividualData = [{
  "account": {
    "id": "001b000003WnPy1AAF",
    "fullName": "jeff bint"
  },
  "positions": [{
    "id": "a16b0000004AxeBAAS",
    "organizationId": "001b0000005gxmlAAA",
    "organizationName": "a",
    "positionTitle": "Senior Manager, Energy",
    "positionLevel": "5-Middle Management & Advisers",
    "isPrimary": true,
    "startDate": "2016-10-07",
    "endDate": null
  }]
}, {
  "account": {
    "id": "0010X000048DDMsQAO",
    "fullName": "edy long"
  },
  "positions": [{
    "id": "a160X000004nKfhQAE",
    "organizationId": "001b0000005gxmlAAA",
    "organizationName": "a",
    "positionTitle": "Managing Director",
    "positionLevel": "4-Head of Business Unit/Head of Region",
    "isPrimary": true,
    "startDate": "2018-03-05",
    "endDate": null
  }]
},  {
    "account": {
      "id": "123",
      "fullName": "john boer"
    },
    "positions": [{
      "id": "325345634634",
      "organizationId": "001b0000005gxmlAAA",
      "organizationName": "a",
      "positionTitle": "Managing Director",
      "positionLevel": "4-Head of Business Unit/Head of Region",
      "isPrimary": true,
      "startDate": "2018-03-05",
      "endDate": null
    }]
  }

]

我当前使用的函数,它确实采用数组的第一个positiontitle字段:

  const updatedGraphTable = { ...graphData,
        engagementAreas: graphData.engagementAreas.map(area => ({ ...area,
          engagementTypes: area.engagementTypes.map(type => ({ ...type,
              engagements: type.engagements.map(engagement => ({ ...engagement,
                members: engagement.members.map(member => ({ ...member,
                  position: { ...member.position,
                    positionTitle: IndividualData[0].positions[0].positionTitle
                  }
                }))
              }))}))
          }))
        };


  console.log(updatedGraphTable)
  console.log('a' + JSON.stringify(updatedGraphTable))

我的预期结果与更新的位置:

updatedGraphData = {

"name": "Annual meetings",
  "engagementAreas": [{
    "id": "1",
    "engagementTypes": [{
        "name": "forestry",
        "engagements": []
      },
      {
        "name": "houses",
        "engagements": [{
          "name": "engagement1",
          "members": [{
              "id": "e334", "account": {
                  "id": "eefe", "fullName": "jim bean"
              },
            "position": {
              "id": "3434",
              "positionTitle": "Manager"
            }
          }]
        }]
      },
 {
  "name": "landscaping",
  "engagements": [{
    "name": "engagement1343",
    "members": [{
        "id": "e334", "account": {
            "id": "123", "fullName": "john boer"
        },
      "position": {
        "id": "4545",
        "positionTitle": "Managing Director"

      }
      }]
     }]
    }
   ]
},

{
"name": "community days",
    "engagementTypes": [{
        "name": "skyscraping",
        "engagements": []
      },
      {
        "name": "tennis",
        "engagements": [{
          "name": "engagement346",
          "members": [{
              "id": "34", "account": {
                  "id": "0010X000048DDMsQAO", "fullName": "edy long"
              },
            "position": {
              "id": "3999434",
              "positionTitle": "Managing Director"
            }
          }]
        }]
      },
 {
  "name": "Juicing",
  "engagements": [{
    "name": "347343",
    "members": [{
        "id": "4546", "account": {
            "id": "001b000003WnPy1AAF", "fullName": "jeff bint"
        },
      "position": {
        "id": "35006",
        "positionTitle": "Senior Manager, Energy"

      }
    }]
}]
 }]
}]
}

我目前的结果:

{
"name": "Annual meetings",
"engagementAreas": [{
    "id": "1",
    "engagementTypes": [{
        "name": "forestry",
        "engagements": []
    }, {
        "name": "houses",
        "engagements": [{
            "name": "engagement1",
            "members": [{
                "id": "e334",
                "account": {
                    "id": "eefe"
                },
                "position": {
                    "id": "3434",
                    "positionTitle": "Senior Manager, Energy"
                }
            }]
        }]
    }, {
        "name": "landscaping",
        "engagements": [{
            "name": "engagement1343",
            "members": [{
                "position": {
                    "id": "4545",
                    "positionTitle": "Senior Manager, Energy"
                }
            }]
        }]
    }]
}, {
    "name": "community days",
    "engagementTypes": [{
        "name": "skyscraping",
        "engagements": []
    }, {
        "name": "tennis",
        "engagements": [{
            "name": "engagement346",
            "members": [{
                "id": "34",
                "account": {
                    "id": "3546"
                },
                "position": {
                    "id": "3999434",
                    "positionTitle": "Senior Manager, Energy"
                }
            }]
        }]
    }, {
        "name": "Juicing",
        "engagements": [{
            "name": "347343",
            "members": [{
                "id": "4546",
                "account": {
                    "id": "3545"
                },
                "position": {
                    "id": "35006",
                    "positionTitle": "Senior Manager, Energy"
                }
            }]
        }]
    }]
}]

}

最佳答案

当然,诀窍是首先将您的数据映射到一个对象中(这样您就不必一直搜索两个数组),然后有条件地设置值(正如我看到您的第一个经理,不确实没有匹配的位置)。

因此,要创建字典以供以后的模型使用,您可以首先执行以下操作

// first map the accountId to positions
const accountIdToPositionDict = individualData.reduce( (current, item) => {
  current[item.account.id] = (item.positions.filter( position => position.isPrimary )[0] || {} ).positionTitle;
  return current;
}, {} );

这将有一个对象,其中 accountIdToPositionDict["123"] 将是 Managing Director,然后将复制逻辑更改为:

const updatedGraphTable = { ...graphData,
  engagementAreas: graphData.engagementAreas.map(area => ({ ...area,
    engagementTypes: area.engagementTypes.map(type => ({ ...type,
        engagements: type.engagements.map(engagement => ({ ...engagement,
          members: engagement.members.map(member => ({ ...member,
            position: { ...member.position,
              // use the found positionTitle, or the original one that was given
              positionTitle: member.account &&  accountIdToPositionDict[member.account.id] || member.position.positionTitle
            }
          }))
        }))}))
    }))
  };

然后根据在字典中找到的 accountId 或原始标题(如果未找到匹配项)设置位置

const individualData = [{
    "account": {
      "id": "001b000003WnPy1AAF",
      "fullName": "jeff bint"
    },
    "positions": [{
      "id": "a16b0000004AxeBAAS",
      "organizationId": "001b0000005gxmlAAA",
      "organizationName": "a",
      "positionTitle": "Senior Manager, Energy",
      "positionLevel": "5-Middle Management & Advisers",
      "isPrimary": true,
      "startDate": "2016-10-07",
      "endDate": null
    }]
  }, {
    "account": {
      "id": "0010X000048DDMsQAO",
      "fullName": "edy long"
    },
    "positions": [{
      "id": "a160X000004nKfhQAE",
      "organizationId": "001b0000005gxmlAAA",
      "organizationName": "a",
      "positionTitle": "Managing Director",
      "positionLevel": "4-Head of Business Unit/Head of Region",
      "isPrimary": true,
      "startDate": "2018-03-05",
      "endDate": null
    }]
  }, {
    "account": {
      "id": "123",
      "fullName": "john boer"
    },
    "positions": [{
      "id": "325345634634",
      "organizationId": "001b0000005gxmlAAA",
      "organizationName": "a",
      "positionTitle": "Managing Director",
      "positionLevel": "4-Head of Business Unit/Head of Region",
      "isPrimary": true,
      "startDate": "2018-03-05",
      "endDate": null
    }]
  }
];

const graphData = {
"name": "Annual meetings",
  "engagementAreas": [{
    "id": "1",
    "engagementTypes": [{
        "name": "forestry",
        "engagements": []
      },
      {
        "name": "houses",
        "engagements": [{
          "name": "engagement1",
          "members": [{
              "id": "e334", "account": {
                  "id": "eefe", "fullName": "jim bean"
              },
            "position": {
              "id": "3434",
              "positionTitle": "Manager"
            }
          }]
        }]
      },
 {
  "name": "landscaping",
  "engagements": [{
    "name": "engagement1343",
    "members": [{
        "id": "e334", "account": {
            "id": "123", "fullName": "john boer"
        },
      "position": {
        "id": "4545",
        "positionTitle": "Senior Manager"

      }
      }]
     }]
    }
   ]
},

{
"name": "community days",
    "engagementTypes": [{
        "name": "skyscraping",
        "engagements": []
      },
      {
        "name": "tennis",
        "engagements": [{
          "name": "engagement346",
          "members": [{
              "id": "34", "account": {
                  "id": "0010X000048DDMsQAO", "fullName": "edy long"
              },
            "position": {
              "id": "3999434",
              "positionTitle": "Ultime Manager"
            }
          }]
        }]
      },
 {
  "name": "Juicing",
  "engagements": [{
    "name": "347343",
    "members": [{
        "id": "4546", "account": {
            "id": "001b000003WnPy1AAF", "fullName": "jeff bint"
        },
      "position": {
        "id": "35006",
        "positionTitle": "Senior Ultimate Manager"

      }
    }]
}]
 }]
}]
};

// first map the accountId to positions
const accountIdToPositionDict = individualData.reduce( (current, item) => {
  current[item.account.id] = (item.positions.filter( position => position.isPrimary )[0] || {} ).positionTitle;
  return current;
}, {} );

// then use it in the mapping function
const updatedGraphTable = { ...graphData,
  engagementAreas: graphData.engagementAreas.map(area => ({ ...area,
    engagementTypes: area.engagementTypes.map(type => ({ ...type,
        engagements: type.engagements.map(engagement => ({ ...engagement,
          members: engagement.members.map(member => ({ ...member,
            position: { ...member.position,
              // use the found positionTitle, or the original one that was given
              positionTitle: member.account &&  accountIdToPositionDict[member.account.id] || member.position.positionTitle
            }
          }))
        }))}))
    }))
  };
  
console.log( updatedGraphTable );

关于javascript - 使用数组中的值更新对象数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52259094/

相关文章:

flash - 在 css 中设置 Flash 对象的样式

python - 如何在 python 中将图像 block 传输到特定图像的区域内?

javascript - 基于另一个对象数组创建对象数组

javascript - 在字符串上定义 getter

java - 将字符串转换为字节数组 (0x) - Java

python - 递归调度算法不能正常工作 Python

c++: 复制一个 boost::array

javascript - 如何将 JSON 转换为索引数组

javascript - 如何在 catch 方法中访问来自 new Error(data) 的数据?

javascript - Angular 表单未提交