2

I have the following:

var identificationIDs = [
{
    "HolderID": "1A000714",
    "TempIssueID": "1A000700"
}
]

Which I am trying to update to include a new object "ExtendedID" with some values to look like below:

var identificationIDs = [
{
    "HolderID": "1A000714",
    "TempIssueID": "1A000700",
    "ExtendedID": [
      "1A000714",      
      "1A000700"
    ]
}
]

Running into issues with trying to push HolderID and TempIssueID into the new object.

Here is my code:

// 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);
1
  • You never create an array to push() into. Also don't want to chain that push() in an assignment statement Commented Jun 3, 2021 at 3:15

2 Answers 2

5

You can use Javascript's built-in spread syntax to help you out.

If you're playing around with arrays, minor changes should be made. Take a look at an example:

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

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


let newIdentificationIds = {...identificationIDs, ExtendedID: extendedId};
console.log(newIdentificationIds)

Sign up to request clarification or add additional context in comments.

Comments

0

You can try these following ways.

var identificationIDs = [
  {
    HolderID: "1A000714",
    TempIssueID: "1A000700",
  },
];

// Using `Object.values`
const result = identificationIDs.map((obj) => ({
  ...obj,
  ExtendedID: Object.values(obj),
}));

// Alternatively, use destructuring
const result2 = identificationIDs.map(({ HolderID, TempIssueID }) => ({
  HolderID,
  TempIssueID,
  ExtendedID: [HolderID, TempIssueID],
}));

console.log(result);
console.log(result2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.