首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JS / Vue:如果一个字段是公共的,则合并两个数组

JS / Vue:如果一个字段是公共的,则合并两个数组
EN

Stack Overflow用户
提问于 2020-10-28 02:22:41
回答 1查看 85关注 0票数 0

我似乎无法管理一种适当的方法来将两个数组合并为一个数组,通过对每个"id“公共字段进行”分组“...

下面是我的最后一次尝试:

代码语言:javascript
复制
// editedItem.extra_tools ///

0:
{
    extra_tool_password: "vpn pwd"
    extra_tool_username: "vpn us"
    id_extra_tool: "8"
},
1:
{
    extra_tool_password: ""
    extra_tool_username: "off"
    id_extra_tool: "1"
},
2:
{
    extra_tool_password: ""
    extra_tool_username: "HS"
    id_extra_tool: "7"
}

应通过id_extra_tool进行合并,以:

代码语言:javascript
复制
// this.extras //
0:
{
extra_params: 1
extra_tool_name: "Hubspot"
extra_tool_password: "password_new"
extra_tool_username: null
id_extra_tool: (1)
}
1:
{
extra_params: 1
extra_tool_name: "Office"
extra_tool_password: "12345"
extra_tool_username: "user_office"
id_extra_tool: (7)
}
{
2:
extra_params: 0
extra_tool_name: (...)
extra_tool_password: (...)
extra_tool_username: (...)
id_extra_tool: (8)
代码语言:javascript
复制
        this.extrasEdited = []
        for (i = this.extras.length - 1; i >= 0; i--) {
          this.extrasEdited.push({
            id_extra_tool: this.editedItem.extra_tools[i].id_extra_tool,
            extra_tool_username: this.editedItem.extra_tools[i].extra_tool_username,
            extra_tool_password: this.editedItem.extra_tools[i].extra_tool_password
          })
          this.editedItem.extra_tools.forEach((element, index) => {
            console.log(element.id_extra_tool, this.extrasEdited[index].id_extra_tool)
            if (element.id_extra_tool === this.extrasEdited[index].id_extra_tool) {
              this.extrasEdited.push({
                extra_params: element.extra_params,
                extra_tool_name: element.extra_tool_name
              })
            }
          })
        }

我想要:

代码语言:javascript
复制
// this.extras //
0:
{
extra_params: 1
extra_tool_name: "Hubspot"
extra_tool_password: "password_new"
extra_tool_username: null
id_extra_tool: 1
}
1:
{
extra_params: 1
extra_tool_name: "Office"
extra_tool_password: "12345"
extra_tool_username: "user_office"
id_extra_tool: 7
}
{
2:
extra_tool_password: "VPN"
extra_tool_username: "HS"
id_extra_tool: "8"
extra_tool_password: "user_VON_pswwww"
extra_tool_username: "user_VPN"

现在应该更清楚了。

泰!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-28 04:55:58

您可以尝试:

代码语言:javascript
复制
var mergedArray =[]
 extra_tools.map(x => {
      extras.map(y => {
        if (x.id_extra_tool === y.id_extra_tool) {
          mergedArray.push(Object.assign(x, y));
        }
      })
    })
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64560670

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档