這個問題在這里已經有了答案: 如何將包含物件的物件轉換為物件陣列 9 個答案 3 小時前關閉。
我想將此處顯示的物件轉換為物件陣列:
我的代碼:
entireObject = {
" 255 638-1527": {
"email": "[email protected]",
"phoneNumber": " 255 638-1527"
},
" 255 532-1587": {
"email": "[email protected]",
"phoneNumber": " 255 532-1587"
},
" 255 613-1587": {
"email": "[email protected]",
"phoneNumber": " 255 613-1587",
"info": [
{
"date": "2022-02-19",
"count": 1
},
{
"date": "2022-03-17",
"count": 9
}]
}
}
我想將其轉換為物件陣列,因此輸出應如下所示:
entireObject = [
{
"email": "[email protected]",
"phoneNumber": " 255 638-1527"
},
"email": "[email protected]",
"phoneNumber": " 255 532-1587"
},
{
"email": "[email protected]",
"phoneNumber": " 255 613-1587",
"info": [
{
"date": "2022-02-19",
"count": 1
},
{
"date": "2022-03-17",
"count": 9
}]
}
}
我需要這樣的資料才能在 HTML 中呈現它,那么我該怎么做呢?
uj5u.com熱心網友回復:
]期望的結果應該以有效的陣列結尾,而不是}. 您所需要的只是Object.values()下面的演示。
const entireObject = {
" 255 638-1527": {
"email": "[email protected]",
"phoneNumber": " 255 638-1527"
},
" 255 532-1587": {
"email": "[email protected]",
"phoneNumber": " 255 532-1587"
},
" 255 613-1587": {
"email": "[email protected]",
"phoneNumber": " 255 613-1587",
"info": [
{
"date": "2022-02-19",
"count": 1
},
{
"date": "2022-03-17",
"count": 9
}]
}
};
const arr = Object.values( entireObject );
console.log( arr );
uj5u.com熱心網友回復:
這個怎么樣?
const myObject = {
" 255 638-1527": {
email: "[email protected]",
phoneNumber: " 255 638-1527"
},
" 255 532-1587": {
email: "[email protected]",
phoneNumber: " 255 532-1587"
},
" 255 613-1587": {
email: "[email protected]",
phoneNumber: " 255 613-1587",
info: [{
date: "2022-02-19",
count: 1
},
{
date: "2022-03-17",
count: 9
}
]
}
};
let objectToArray = [{ ...myObject
}]
console.log(objectToArray)
uj5u.com熱心網友回復:
只是想為您指出正確的方向,因為提出了類似的問題并得到了完美的回答。這是鏈接:如何將包含物件的物件轉換為物件陣列 注意,請確保查看被選為最佳答案的答案。
uj5u.com熱心網友回復:
你可以這樣做:
const myObject = {
" 255 638-1527": {
email: "[email protected]",
phoneNumber: " 255 638-1527"
},
" 255 532-1587": {
email: "[email protected]",
phoneNumber: " 255 532-1587"
},
" 255 613-1587": {
email: "[email protected]",
phoneNumber: " 255 613-1587",
info: [{
date: "2022-02-19",
count: 1
},
{
date: "2022-03-17",
count: 9
}
]
}
};
let result = Object.values(myObject).map((item) => ({ ...item
}));
console.log(result);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/439296.html
標籤:javascript 节点.js 数组 json 目的
