如何将对象的对象组合成单个对象 Angular

您好,我有一个对象的对象,我需要合并到单个对象中,下面是对象的对象:


{

  0: {

    CLIENTS_EMAIL: null

  }

  1: {

    CLIENTS_PASSWORD: null

  }

  2: {

    CLIENTS_CONFIRMPASSWORD: null

  }

  3: {

    CLIENTS_FIRSTNAME: null

  }

  4: {

    CLIENTS_LASTNAME: null

  }

  5: {

    CLIENTS_MOBILE: null

  }

  6: {

    CLIENTS_HOMEPHONE: null

  }

  7: {

    CLIENTS_BUSPHONE: null

  }

  8: {

    CLIENTS_RECEIVE_MARKETING_VIA: [

      0: {

        CLIENTS_ISMARKETINGVIAEMAIL: false

      }

      1: {

        CLIENTS_ISMARKETINGVIAMAIL: false

      }

      2: {

        CLIENTS_ISMARKETINGVIAPHONE: false

      }

      3: {

        CLIENTS_ISMARKETINGVIASMS: false

      }

    ]

  }

}

您好,我有一个对象的对象,我需要合并到单个对象中,下面是对象的对象:


{

  0: {

    CLIENTS_EMAIL: null

  }

  1: {

    CLIENTS_PASSWORD: null

  }

  2: {

    CLIENTS_CONFIRMPASSWORD: null

  }

  3: {

    CLIENTS_FIRSTNAME: null

  }

  4: {

    CLIENTS_LASTNAME: null

  }

  5: {

    CLIENTS_MOBILE: null

  }

  6: {

    CLIENTS_HOMEPHONE: null

  }

  7: {

    CLIENTS_BUSPHONE: null

  }

  8: {

    CLIENTS_RECEIVE_MARKETING_VIA: [

      0: {

        CLIENTS_ISMARKETINGVIAEMAIL: false

      }

      1: {

        CLIENTS_ISMARKETINGVIAMAIL: false

      }

      2: {

        CLIENTS_ISMARKETINGVIAPHONE: false

      }

      3: {

        CLIENTS_ISMARKETINGVIASMS: false

      }

    ]

  }

}

展开片段

需要将其添加到单个对象中,下面是示例:


let data = {

  

CLIENTS_EMAIL: null,

CLIENTS_PASSWORD: null,

CLIENTS_CONFIRMPASSWORD: null,

CLIENTS_FIRSTNAME: null,

CLIENTS_LASTNAME: null,

CLIENTS_MOBILE: null,

CLIENTS_HOMEPHONE: null,

CLIENTS_BUSPHONE: null,

CLIENTS_ISMARKETINGVIAEMAIL: false,

CLIENTS_ISMARKETINGVIAMAIL: false,

CLIENTS_ISMARKETINGVIAPHONE: false,

CLIENTS_ISMARKETINGVIASMS: false,

  

}


以下是我正在尝试的逻辑:


const data = Object.entries(Object.entries(object).map((data: any) => {

  return data[1]

}))

还查看了其他其他功能,但没有成功



慕神8447489
浏览 105回答 2
2回答

暮色呼如

这是演示有趣的递归问题。但源数据有点奇怪。数组项不是键值对象。应该是这样的:var a = {    0: {        CLIENTS_EMAIL: null    },    1: {        CLIENTS_PASSWORD: null    },    2: {        CLIENTS_CONFIRMPASSWORD: null    },    3: {        CLIENTS_FIRSTNAME: null    },    4: {        CLIENTS_LASTNAME: null    },    5: {        CLIENTS_MOBILE: null    },    6: {        CLIENTS_HOMEPHONE: null    },    7: {        CLIENTS_BUSPHONE: null    },    8: {        CLIENTS_RECEIVE_MARKETING_VIA: [            {                CLIENTS_ISMARKETINGVIAEMAIL: false            },            {                CLIENTS_ISMARKETINGVIAMAIL: false            },            {                CLIENTS_ISMARKETINGVIAPHONE: false            },            {                CLIENTS_ISMARKETINGVIASMS: false            }        ]    }}然后使用递归let result = {};let convert = (obj) => {    for (var i in obj) {        if (obj[i] != null && obj[i] !== false) {            convert(obj[i]);        } else {            result[i] = obj[i];        }    }}

米脂

该函数递归地将对象的对象合并为单个对象,但成本太高。时间复杂度约为O(n 2 log n)(虽然没有测试其他输入,所以让我知道它是否不适用于所有内容)let result = {};recMerge(ooo); /// --> 'ooo' is your (o)bject (o)f (o)bjectsfunction recMerge(obj) {  Object.getOwnPropertyNames(obj).forEach((prop) => {    const propName = obj[prop];    Object.getOwnPropertyNames(propName).forEach((propValue) => {      if (!!propName[propValue] && typeof propName[propValue] === "object") {        recMerge(propName[propValue]);      } else {        result = { ...result, ...propName };      }    });  });}例子输出将始终是每次迭代中最内部的对象,并分布到单个对象中。CLIENTS_RECEIVE_MARKETING_VIA: {  0: {    CLIENTS_ISMARKETINGVIAEMAIL: false,  },  1: {    CLIENTS_ISMARKETINGVIAMAIL: false,  },  2: {    CLIENTS_ISMARKETINGVIAPHONE: false,  },  3: {    CLIENTS_ISMARKETINGVIASMS: {      0: {        CLIENTS_ISMARKETINGVIATEXT: false,      },      1: {        CLIENTS_ISMARKETINGVIAPIC: false,      },    },  },},会输出{  CLIENTS_ISMARKETINGVIAEMAIL: false,  CLIENTS_ISMARKETINGVIAMAIL: false,  CLIENTS_ISMARKETINGVIAPHONE: false,  CLIENTS_ISMARKETINGVIATEXT: false,  CLIENTS_ISMARKETINGVIAPIC: false,}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript