這就是我當前物件的樣子。 我的問題是如何決議這個物件,使其回傳預期的輸出(沒有索引 0,1,2...)我想要一個 Javascript 物件陣列,沒有鍵值。
"contacts": {
"0": {
"firstName": "James",
"lastName": "April",
"emails": [
{
"emailType": "WORK",
"address": ""
},
{
"emailType": "PERSONAL",
"address": ""
}
],
"relationship": "boyfriend",
"phones": [
{
"phoneType": "HOME",
"phoneNumber": "(456) 888-9999"
},
{
"phoneType": "CELL",
"phoneNumber": "(789) 123-4567"
},
],
"callSequence": 0,
"note": null
},
"1": {
"firstName": "Joe",
"lastName": "Kimmel",
"emails": [
{
"emailType": "WORK",
"address": ""
},
{
"emailType": "PERSONAL",
"address": ""
}
],
"relationship": "ex-husband",
"phones": [
{
"phoneType": "HOME",
"phoneNumber": ""
},
{
"phoneType": "CELL",
"phoneNumber": "(111) 111-1111"
},
{
"phoneType": "WORK",
"phoneNumber": ""
}
],
"callSequence": 0,
"note": null
},
"2": {
"firstName": "Test",
"lastName": "",
"emails": [
{
"emailType": "WORK",
"address": "[email protected]"
},
{
"emailType": "PERSONAL",
"address": "[email protected]"
}
],
"relationship": "BROTHER-IN-LAW",
"phones": [
{
"phoneType": "HOME",
"phoneNumber": ""
},
{
"phoneType": "CELL",
"phoneNumber": ""
},
{
"phoneType": "WORK",
"phoneNumber": ""
}
],
"callSequence": 0
}
}
}
預期結果
沒有索引 0,1,2 并且應該在陣列內。所以我希望輸出為物件陣列
"contacts": [{
{
"firstName": "James",
"lastName": "April",
"emails": [
{
"emailType": "WORK",
"address": ""
},
{
"emailType": "PERSONAL",
"address": ""
}
],
"relationship": "boyfriend",
"phones": [
{
"phoneType": "HOME",
"phoneNumber": "(456) 888-9999"
},
{
"phoneType": "CELL",
"phoneNumber": "(789) 123-4567"
},
],
"callSequence": 0,
"note": null
},
{
"firstName": "Joe",
"lastName": "Kimmel",
"emails": [
{
"emailType": "WORK",
"address": ""
},
{
"emailType": "PERSONAL",
"address": ""
}
],
"relationship": "ex-husband",
"phones": [
{
"phoneType": "HOME",
"phoneNumber": ""
},
{
"phoneType": "CELL",
"phoneNumber": "(111) 111-1111"
},
{
"phoneType": "WORK",
"phoneNumber": ""
}
],
"callSequence": 0,
"note": null
}]
uj5u.com熱心網友回復:
您可以使用 Object.values 來獲取您想要的陣列。
const newObject = {contacts: Object.values(oldObject.contacts)};
uj5u.com熱心網友回復:
僅Object.values用于聯系人屬性
let data = {"contacts": {
"0": {
"firstName": "James",
"lastName": "April",
"emails": [
{
"emailType": "WORK",
"address": ""
},
{
"emailType": "PERSONAL",
"address": ""
}
],
"relationship": "boyfriend",
"phones": [
{
"phoneType": "HOME",
"phoneNumber": "(456) 888-9999"
},
{
"phoneType": "CELL",
"phoneNumber": "(789) 123-4567"
},
],
"callSequence": 0,
"note": null
},
"1": {
"firstName": "Joe",
"lastName": "Kimmel",
"emails": [
{
"emailType": "WORK",
"address": ""
},
{
"emailType": "PERSONAL",
"address": ""
}
],
"relationship": "ex-husband",
"phones": [
{
"phoneType": "HOME",
"phoneNumber": ""
},
{
"phoneType": "CELL",
"phoneNumber": "(111) 111-1111"
},
{
"phoneType": "WORK",
"phoneNumber": ""
}
],
"callSequence": 0,
"note": null
},
"2": {
"firstName": "Test",
"lastName": "",
"emails": [
{
"emailType": "WORK",
"address": "[email protected]"
},
{
"emailType": "PERSONAL",
"address": "[email protected]"
}
],
"relationship": "BROTHER-IN-LAW",
"phones": [
{
"phoneType": "HOME",
"phoneNumber": ""
},
{
"phoneType": "CELL",
"phoneNumber": ""
},
{
"phoneType": "WORK",
"phoneNumber": ""
}
],
"callSequence": 0
}
}
}
data.contacts = Object.values(data.contacts);
console.log(data)
uj5u.com熱心網友回復:
Object.values ()方法回傳給定物件自己的可列舉屬性值的陣列。在這種情況下,要傳遞的物件contacts來自原始資料。
var data = { "contacts": { "0": { "firstName": "James", "lastName": "April", "emails": [ { "emailType": "WORK", "address": "" }, { "emailType": "PERSONAL", "address": "" } ], "relationship": "boyfriend", "phones": [ { "phoneType": "HOME", "phoneNumber": "(456) 888-9999" }, { "phoneType": "CELL", "phoneNumber": "(789) 123-4567" }, ], "callSequence": 0, "note": null }, "1": { "firstName": "Joe", "lastName": "Kimmel", "emails": [ { "emailType": "WORK", "address": "" }, { "emailType": "PERSONAL", "address": "" } ], "relationship": "ex-husband", "phones": [ { "phoneType": "HOME", "phoneNumber": "" }, { "phoneType": "CELL", "phoneNumber": "(111) 111-1111" }, { "phoneType": "WORK", "phoneNumber": "" } ], "callSequence": 0, "note": null }, "2": { "firstName": "Test", "lastName": "", "emails": [ { "emailType": "WORK", "address": "[email protected]" }, { "emailType": "PERSONAL", "address": "[email protected]" } ], "relationship": "BROTHER-IN-LAW", "phones": [ { "phoneType": "HOME", "phoneNumber": "" }, { "phoneType": "CELL", "phoneNumber": "" }, { "phoneType": "WORK", "phoneNumber": "" } ], "callSequence": 0 } } };
data = { contacts: Object.values(data.contacts) }
console.log(data)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/414887.html
標籤:
下一篇:Certbot極致挑戰
