从 2 个现有对象创建新对象

JS_*_*dge 2 javascript arrays object

我有以下内容:

var identificationIDs = [
{
    "HolderID": "1A000714",
    "TempIssueID": "1A000700"
}
]
Run Code Online (Sandbox Code Playgroud)

我正在尝试更新以包含一个新对象“ExtendedID”,其中一些值如下所示:

var identificationIDs = [
{
    "HolderID": "1A000714",
    "TempIssueID": "1A000700",
    "ExtendedID": [
      "1A000714",      
      "1A000700"
    ]
}
]
Run Code Online (Sandbox Code Playgroud)

尝试将 HolderID 和 TempIssueID 推入新对象时遇到问题。

这是我的代码:

// Simplify variable name:
var userID = identificationIDs;

// Create new object and assign values:
for (var i = 0; i < userID.length; i++) {
   
    userID[i].HolderID = userID[i].ID;
    userID[i].ExtendedID = userID[i].HolderID.push(TempIssueID);
}

console.log(userID);
Run Code Online (Sandbox Code Playgroud)

tes*_*_22 5

您可以使用 Javascript 的内置扩展语法来帮助您。

如果您正在使用数组,则应该进行一些细微的更改。看一个例子:

let identificationIDs = {
    "HolderID": "1A000714",
    "TempIssueID": "1A000700"
}

let extendedId = [
      "1A000714",      
      "1A000700"
    ]


let newIdentificationIds = {...identificationIDs, ExtendedID: extendedId};
console.log(newIdentificationIds)
Run Code Online (Sandbox Code Playgroud)