[英]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.