我从API获取这种格式的 JOSN 。我在单独的键中获得列和行。可以将列和行连接到单个对象。
{
"datasource": "xxxxxxxxxxxxxxxx",
"columns": [
"Last Activity Date-Time",
"Name",
"City",
"Age",
"Gender",
"Initial Inquiry Date",
"Status",
"Anticipated_Move-in",
"LastContacted",
"NextStep",
"Lead Score"
],
"rows": [
[
"2019-03-07T12:56:00",
"Harold Glucks",
"",
"",
"",
"03/05/2019",
"LOST (No Potential for Move-In)",
"",
"03/07/2019",
"Email ",
"100"
],
[
"2018-05-14T10:23:00",
"Lea Deitch",
"",
"",
"Female",
"05/17/2017",
"LOST (No Potential for Move-In)",
"",
"05/14/2018",
"Phone Call - Lead",
"100"
],
[
"2019-02-08T13:19:00",
"Donna Snoderly",
"",
"",
"",
"08/22/2018",
"WARM (Move-In Likely in 30-60 Days)",
"",
"02/08/2019",
"Email ",
"100"
]
],
"numRows": 3,
"numColumns": 11,
"fromcache": true
}
我尝试将列添加到数组,将行添加到另一个数组。合并此数组没有达到我的预期。 我想将对象组织成这种格式,这可能吗?
[
{
Last Activity Date-Time : "2019-03-07T12:56:00",
Name: "Harold Glucks",
City: "",
Age: "",
Gender: "",
Initial Inquiry Date: "03/05/2019",
Status: "LOST (No Potential for Move-In)",
Anticipated_Move-in: "",
LastContacted: "03/07/2019",
NextStep: "Email",
Lead Score: "100"
},
{
Last Activity Date-Time : "2019-03-07T12:56:00",
Name: "Harold Glucks",
City: "",
Age: "",
Gender: "",
Initial Inquiry Date: "03/05/2019",
Status: "LOST (No Potential for Move-In)",
Anticipated_Move-in: "",
LastContacted: "03/07/2019",
NextStep: "Email",
Lead Score: "100"
},
{
Last Activity Date-Time : "2019-03-07T12:56:00",
Name: "Harold Glucks",
City: "",
Age: "",
Gender: "",
Initial Inquiry Date: "03/05/2019",
Status: "LOST (No Potential for Move-In)",
Anticipated_Move-in: "",
LastContacted: "03/07/2019",
NextStep: "Email",
Lead Score: "100"
}
]
答案 0 :(得分:2)
您可以映射行并映射列作为键并获取对象。
var data = { datasource: "xxxxxxxxxxxxxxxx", columns: ["Last Activity Date-Time", "Name", "City", "Age", "Gender", "Initial Inquiry Date", "Status", "Anticipated_Move-in", "LastContacted", "NextStep", "Lead Score"], rows: [["2019-03-07T12:56:00", "Harold Glucks", "", "", "", "03/05/2019", "LOST (No Potential for Move-In)", "", "03/07/2019", "Email", "100"], ["2018-05-14T10:23:00", "Lea Deitch", "", "", "Female", "05/17/2017", "LOST (No Potential for Move-In)", "", "05/14/2018", "Phone Call - Lead", "100"], ["2019-02-08T13:19:00", "Donna Snoderly", "", "", "", "08/22/2018", "WARM (Move-In Likely in 30-60 Days)", "", "02/08/2019", "Email", "100"]], numRows: 3, numColumns: 11, fromcache: true }
result = data.rows.map(a => Object.assign(...data.columns.map((k, i) => ({ [k]: a[i] }))));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:0)
使用lodash:
const obj = JSON.parse(json);
const merged = _.map(obj.rows, (r, i) => {
return _.zipObject(obj.columns, r);
});