[英]JavaScript: Filter out keys from array of objects
這就是我現在的 object 的樣子。 我的問題是如何解析這個 object 以便它返回預期的 output (沒有索引 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": "test@gmail.com"
},
{
"emailType": "PERSONAL",
"address": "test2@gmail.com"
}
],
"relationship": "BROTHER-IN-LAW",
"phones": [
{
"phoneType": "HOME",
"phoneNumber": ""
},
{
"phoneType": "CELL",
"phoneNumber": ""
},
{
"phoneType": "WORK",
"phoneNumber": ""
}
],
"callSequence": 0
}
}
}
預期結果
沒有索引 0,1,2 並且應該在一個數組中。 所以我想要 output 作為對象數組
"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
}]
您可以使用 Object.values 來獲取您想要的數組。
const newObject = {contacts: Object.values(oldObject.contacts)};
只需將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": "test@gmail.com" }, { "emailType": "PERSONAL", "address": "test2@gmail.com" } ], "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)
Object.values()方法返回給定對象自己的可枚舉屬性值的數組。 在這種情況下,要傳遞的 object 是來自原始數據的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": "test@gmail.com" }, { "emailType": "PERSONAL", "address": "test2@gmail.com" } ], "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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.