您好我想檢查是否有辦法將我的 JSON 屬性值從字串轉換為字串陣列。
我有的:
const testJSON = {
"name": "Albert"
}
我想要的是:
const testJSON = {
"name": ["Albert"]
}
非常感謝我可以探索的任何幫助和方向!
uj5u.com熱心網友回復:
您可以將可以是字串或陣列的新值分配給 json 屬性。
例如:
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;
uj5u.com熱心網友回復:
Object.keys()您可以在andArray.forEach()方法的幫助下嘗試這種動態解決方案。
const testJSON = {
"name": "Albert"
};
Object.keys(testJSON).forEach(key => {
testJSON[key] = [testJSON[key]]
});
console.log(testJSON);
uj5u.com熱心網友回復:
不要這么認為,你可能只是喜歡上面的評論
{name: [testJSON.name]}
如果你有陣列,你可以為它做 .map
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/525295.html
標籤:javascript数组
上一篇:無法將psql插入本地轉發的dockerpostgres影像埠
下一篇:嘗試遍歷陣列并注釋它們
