我有下面的数组,我转换为Object但我需要的只是一些键/值对。为此,我正在寻找仅使用所需键创建对象。
例如:
最终结果仅包括 1 - 开始 2 - UserId, 3 - 持续时间
for (var counter = 0; counter < rows.length; counter++) {
newObj[counter].Start = oldObj[counter].starttime;
newObj[counter].UseId = oldObj[counter].card_id;
newObj[counter].Duration = oldObj[counter].sessiontime;
}
[
{
"id": 52,
"sessionid": "SIP/7631253675-0003e946",
"uniqueid": "1498843959.256441",
"card_id": 1,
"nasipaddress": "",
"starttime": "2017-06-30T16:32:58.000Z",
"stoptime": "2017-06-30T16:33:07.000Z",
"sessiontime": 9,
"calledstation": "447454411749",
"sessionbill": 1.5,
"id_tariffgroup": 1,
"id_tariffplan": 1,
"id_ratecard": 2,
"id_trunk": 2,
"sipiax": 0,
"src": "2638645541115",
"id_did": null,
"buycost": 0.05,
"id_card_package_offer": null,
"real_sessiontime": 9,
"dnid": "0044745881749",
"terminatecauseid": 1,
"destination": 44,
"a2b_custom1": "",
"a2b_custom2": ""
},
{
"id": 53,
"sessionid": "SIP/7631253675-0003e949",
"uniqueid": "1498844032.256444",
"card_id": 1,
"nasipaddress": "",
"starttime": "2017-06-30T16:34:16.000Z",
"stoptime": "2017-06-30T16:34:21.000Z",
"sessiontime": 5,
"calledstation": "447438811813",
"sessionbill": 1.5,
"id_tariffgroup": 1,
"id_tariffplan": 1,
"id_ratecard": 2,
"id_trunk": 2,
"sipiax": 0,
"src": "2638644991115",
"id_did": null,
"buycost": 0.05,
"id_card_package_offer": null,
"real_sessiontime": 5,
"dnid": "00447466811813",
"terminatecauseid": 1,
"destination": 44,
"a2b_custom1": "",
"a2b_custom2": ""
}
]
你可以帮我解决一下吗?此致
答案 0 :(得分:1)
您可以使用array#map
并在迭代原始对象数组并从每个对象中提取值时创建对象。
var data = [{ "id": 52, "sessionid": "SIP/7631253675-0003e946", "uniqueid": "1498843959.256441", "card_id": 1, "nasipaddress": "", "starttime": "2017-06-30T16:32:58.000Z", "stoptime": "2017-06-30T16:33:07.000Z", "sessiontime": 9, "calledstation": "447454411749","sessionbill": 1.5, "id_tariffgroup": 1, "id_tariffplan": 1, "id_ratecard": 2, "id_trunk": 2, "sipiax": 0, "src": "2638645541115", "id_did": null, "buycost": 0.05, "id_card_package_offer": null, "real_sessiontime": 9, "dnid": "0044745881749", "terminatecauseid":1, "destination": 44, "a2b_custom1": "", "a2b_custom2": "" }, { "id": 53, "sessionid": "SIP/7631253675-0003e949", "uniqueid": "1498844032.256444", "card_id": 1, "nasipaddress": "", "starttime": "2017-06-30T16:34:16.000Z", "stoptime": "2017-06-30T16:34:21.000Z","sessiontime": 5, "calledstation": "447438811813", "sessionbill": 1.5, "id_tariffgroup": 1, "id_tariffplan": 1, "id_ratecard": 2, "id_trunk": 2, "sipiax": 0, "src": "2638644991115", "id_did": null, "buycost": 0.05, "id_card_package_offer": null, "real_sessiontime":5, "dnid": "00447466811813", "terminatecauseid": 1, "destination": 44, "a2b_custom1": "", "a2b_custom2": "" } ],
result = data.map(o => ({Start: o.starttime, UseId: o.card_id, Duration: o.sessiontime}));
console.log(result);
答案 1 :(得分:0)
上面的代码会给你一个错误。您正在尝试访问newObj数组的第n个元素,该元素尚未初始化。
您可以将代码编写为
for (var counter = 0; counter < rows.length; counter++) {
newObj.push({});
newObj[counter].Start = oldObj[counter].starttime;
newObj[counter].UseId = oldObj[counter].card_id;
newObj[counter].Duration = oldObj[counter].sessiontime;
}