Home > front end >  Can I change a JSON key from string value to an array of value in JS
Can I change a JSON key from string value to an array of value in JS

Time:11-02

Hi I would like to check if there's a way to convert one of my JSON property's value from string to an array of string.

What I have:

const testJSON = {
  "name": "Albert"
}

What I want:

const testJSON = {
  "name": ["Albert"]
}

Many thanks for any help and direction I could explore!

CodePudding user response:

You can assign new value which can be string or array to json property.

For example:

const testJSON = {
  "name": "Albert"
}

// new array which we will assign to previous array property
const newArray = ["Albert", "John"];

// assigning new array to old json property
testJSON.name = newArray;

CodePudding user response:

Don't think so, you only do maybe like the comment above

 {name: [testJSON.name]}

if you have array, u can do .map for it

  • Related