我正在尝试将此嵌套数组数据转换为对象数组。这是数据。
const tableData = [
["first_name", "last_name", "city", "state"],
["June", "Gregory", "Hammond", "IN"],
["James", "Wynn", "Gary", "IN"],
["Craig", "Norman", "Schererville", "IN"]
]
这是输出内容
[
{ first_name : "June", last_name : "Gregory", city: "Hammond", state : "IN" },
{ first_name : "James", last_name : "Wynn", city: "Gary", state : "IN" },
{ first_name : "Craig", last_name : "Norman", city: "Schererville", state : "IN" }
]
这是我的代码。
function convertTable(table_data) {
var result = []
var key, value
for (var i=0; i<table_data.length; i++) {
var employee = {}
for (var j=0; j<table_data[i].length; j++) {
key = table_data[i][j][0]
value = table_data[i][j][1]
employee[key] = value
}
result.push(employee)
}
return result
}
var input = [
["first_name", "last_name", "city", "state"],
["June", "Gregory", "Hammond", "IN"],
["James", "Wynn", "Gary", "IN"],
["Craig", "Norman", "Schererville", "IN"]
]
console.log(convertTable(input));
解决方案是打印出单词的前字母而不是整个单词。我很难理解为什么。我问是否有评论者可以逐行留下评论,以帮助我理解该程序。我在研究类似问题的帮助下编写了该书,但仍然很难理解。我一直在阅读有关w3schools,MDN上的Javascript的内容,并观看有关解释的Youtube视频。任何其他帮助,将不胜感激。
答案 0 :(得分:0)
首先,您将两个值都用一个字符代替了,
class
<select name="dnn$ctr6707$TimeTableView$ClassesList" id="dnn_ctr6707_TimeTableView_ClassesList" class="HeaderClasses">
<option selected="selected" value="14">a</option>
<option value="15">b</option>
<option value="16">c</option>
<option value="17">d</option>
<option value="49">e</option>
<option value="60">f</option>
</select>
<hr/>
<input id="inp" type="text" value="0"/>
<button id="select">Select</button>
然后您需要进行两项更改,其中一项用于使外循环省略键,而内部则使第一行用于键。请在代码中查看注释。
另一个变化是在函数顶部声明所有变量。
key = table_data[i][j][0]
// ^^^
value = table_data[i][j][1]
// ^^^
function convertTable(table_data) {
var result = [],
key, value,
employee,
i, j;
for (i = 1; i < table_data.length; i++) { // start from index 1
employee = {};
for (j = 0; j < table_data[i].length; j++) {
key = table_data[0][j]; // take the value form index zero
// no following index for the letter
value = table_data[i][j]; // no following index for the letter
employee[key] = value;
}
result.push(employee);
}
return result;
}
var input = [["first_name", "last_name", "city", "state"], ["June", "Gregory", "Hammond", "IN"], ["James", "Wynn", "Gary", "IN"], ["Craig", "Norman", "Schererville", "IN"]]
console.log(convertTable(input));
不同的方法
.as-console-wrapper { max-height: 100% !important; top: 0; }
function convertTable(table) {
return table
.slice(1)
.map(a => Object.assign(...table[0].map((k, i) => ({ [k]: a[i] }))));
}
var input = [["first_name", "last_name", "city", "state"], ["June", "Gregory", "Hammond", "IN"], ["James", "Wynn", "Gary", "IN"], ["Craig", "Norman", "Schererville", "IN"]]
console.log(convertTable(input));
答案 1 :(得分:0)
尝试一下
您只是放错了一些索引,只是一切都很好。 只需与您的功能匹配,您就可以轻松找到错过的地方。
const tableData = [
["first_name", "last_name", "city", "state"],
["June", "Gregory", "Hammond", "IN"],
["James", "Wynn", "Gary", "IN"],
["Craig", "Norman", "Schererville", "IN"]
]
function convertTable(table_data) {
var result = []
var key, value
for (var i=1; i<table_data.length; i++) {
var employee = {}
for (var j=0; j<table_data[i].length; j++) {
key = table_data[0][j]
value = table_data[i][j]
employee[key] = value
}
result.push(employee)
}
return result
}
var input = [
["first_name", "last_name", "city", "state"],
["June", "Gregory", "Hammond", "IN"],
["James", "Wynn", "Gary", "IN"],
["Craig", "Norman", "Schererville", "IN"]
]
console.log(convertTable(input));
{ first_name : "June", last_name : "Gregory", city: "Hammond", state : "IN" },
{ first_name : "James", last_name : "Wynn", city: "Gary", state : "IN" },
{ first_name : "Craig", last_name : "Norman", city: "Schererville", state : "IN" }
] */
答案 2 :(得分:0)
您正在访问根数组上的3-level
深度,因此您可以在2-level
数组中索引字符串。
使用此方法:key = table_data[i][j][0]
您正在访问索引j-placed
上的数组内i
字符串的第一个字符。
并使用以下命令:value = table_data[i][j][1]
正在访问索引j-placed
上的数组内i
字符串的第二个字符。
此外,您已经知道第一个数组是键(或标头),因此您不想在其上循环。检查下一个示例:
function convertTable(table_data)
{
var result = [];
var key, value;
for (var i = 1; i < table_data.length; i++)
{
var employee = {};
for (var j = 0; j < table_data[i].length; j++)
{
key = table_data[0][j];
value = table_data[i][j];
employee[key] = value;
}
result.push(employee);
}
return result;
}
var input = [
["first_name", "last_name", "city", "state"],
["June", "Gregory", "Hammond", "IN"],
["James", "Wynn", "Gary", "IN"],
["Craig", "Norman", "Schererville", "IN"]
]
console.log(convertTable(input));
答案 3 :(得分:0)
您的代码包含三个错误,可以通过以下方式修复:
从i = 1
开始迭代您的外循环以跳过标头(索引为0)。
使用tableData[0][j]
而不是tableData[i][j][0]
进入标头数组的索引,这是一个维度过多(访问字符),并且错误地包含了行索引i
。
使用tableData[i][j]
而不是tableData[i][j][1]
索引数据表,它再次访问字符串中的字符。
除此以外,我建议注意间距,分号和驼峰式惯例。这将使您的代码更易于理解和调试。
将其放在一起将产生:
function convertTable(tableData) {
var result = [];
var key;
var value;
for (var i = 1; i < tableData.length; i++) { // start at 1 to skip headers
var employee = {};
for (var j = 0; j < tableData[i].length; j++) {
key = tableData[0][j]; // remove character index and use [0][j]
value = tableData[i][j]; // remove character index
employee[key] = value;
}
result.push(employee);
}
return result;
}
var input = [
["first_name", "last_name", "city", "state"],
["June", "Gregory", "Hammond", "IN"],
["James", "Wynn", "Gary", "IN"],
["Craig", "Norman", "Schererville", "IN"]
];
console.log(convertTable(input));
除此之外,对于数组函数map
和reduce
来说,执行CSV到对象的转换是一项艰巨的任务。 shift()
将标题从数组的前面弹出,map
遍历行并将reduce
应用于每个行。 reduce
使用i
和j
行/列索引来构建对象,以与函数相同的方式引用标头数组。
const tableData = [
["first_name", "last_name", "city", "state"],
["June", "Gregory", "Hammond", "IN"],
["James", "Wynn", "Gary", "IN"],
["Craig", "Norman", "Schererville", "IN"]
];
const headers = tableData.shift();
const result = tableData.map((e, i) =>
e.reduce((a, f, j) => {
a[headers[j]] = f;
return a;
}, {})
);
console.log(result);
答案 4 :(得分:0)
以下是使用Array.reduce
的简洁版本:
const data = [["first_name", "last_name", "city", "state"], ["June", "Gregory", "Hammond", "IN"], ["James", "Wynn", "Gary", "IN"], ["Craig", "Norman", "Schererville", "IN"]]
const result = data.reduce((r,c,i,arr) => {
if(i) r.push(c.reduce((a,b,j) => (a[arr[0][j]] = b, a), {}))
return r
}, [])
console.log(result)
想法是通过跳过第一行/标题行的数据来处理数据,然后继续将内部reduce
的组合对象推入累加器,内部merge commit
只是用标题中的值装饰和清空对象(作为键) )和当前数组(作为值)