【问题标题】:merging the contents of an array with a single value from an object将数组的内容与对象的单个值合并
【发布时间】:2023-02-11 10:22:06
【问题描述】:

我在将对象与数组内容合并时遇到问题。我想将 array1(仅标题)中的值合并到数组中。我还需要从标题中重命名该字段。

const array = [{ name: 'bob'},{ name: 'don' }]

const array1 = [[{ title: 'director', dob: 'xxx'}] ,[{ title: 'manager', dob: 'xxx}]]

我试图用数组做这样的事情:

array.push({'new_title_name': array1.map((item) => item.title)})

它给了我一大堆骗局,而且看起来有点老套。

提前致谢, 吉米

【问题讨论】:

  • array1 是数组的数组有点令人困惑。嵌套数组中可能有多个条目吗?在您的示例中,嵌套数组中只有一个条目

标签: javascript typescript


【解决方案1】:

你可以做

const array = [{ name: 'bob'},{ name: 'don' }]

const array1 = [[{ title: 'director', dob: 'xxx'}] ,[{ title: 'manager', dob: 'xxx'}]]

console.log(array.map((el, i) => ({...el, new_title_name: array1[i][0].title })))

【讨论】:

    猜你喜欢
    • 2017-02-06
    • 1970-01-01
    • 2016-06-14
    • 2015-12-21
    • 1970-01-01
    • 2022-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多