[英]API response is array of arrays. The first array being the key in key-value pair of an object
这就是我的流程。 我将使用第一个数组,将值作为对象键分配给我的模板。 然后,我想使用该模板,将下一个数组的元素分配为其值,然后将其推入最终响应对象。 然后,我将继续执行此操作,直到到达响应数组的末尾。 我本质上想将其转换为JSON,使用第一个响应数组的元素作为所有值。
这是回应:
[
[
"POP",
"GEONAME",
"state"
],
[
"4863300",
"Alabama",
"01"
],
[
"741894",
"Alaska",
"02"
],
[
"6931071",
"Arizona",
"04"
],
[
"2988248",
"Arkansas",
"05"
],
[
"39250017",
"California",
"06"
]
]
这是我想要的输出(键始终是第一个响应索引)
{
{
"POP": "4863300"
"GEONAME": "Alabama"
"state": "01"
}
{
"POP": "741894"
"GEONAME": "Alaska"
"state": "02"
},
{
"POP": "6931071"
"GEONAME": "Arizona"
"state": "04"
},
{
"POP": "2988248"
"GEONAME": "Arkansas"
"state": "05"
},
{
"POP": "39250017"
"GEONAME": "California"
"state": "06"
}
}
这是我到目前为止的内容:
function modifyArrayResponse(response) {
// Create template (Assign keys)
let template = {};
let keys = response[0];
// Assign keys to template
for(let i = 0; i < keys.length; i++){
template[keys[i]] = template[i];
}
// Use the template (Assign values)
// Return modified response
}
您想要的输出无效。 整个过程中都有{}
,但这是针对对象的,它必须是key: value
对。 您应该期望的是一系列对象:
[
{
"POP": "4863300"
"GEONAME": "Alabama"
"state": "01"
}
{
"POP": "741894"
"GEONAME": "Alaska"
"state": "02"
},
{
"POP": "6931071"
"GEONAME": "Arizona"
"state": "04"
},
{
"POP": "2988248"
"GEONAME": "Arkansas"
"state": "05"
},
{
"POP": "39250017"
"GEONAME": "California"
"state": "06"
}
]
创建代码的代码需要在第一个之后的所有元素上循环。
function modifyArrayResponse(response) { const keys = response[0]; const result = []; for (let i = 1; i < response.length; i++) { const obj = {}; keys.forEach((key, index) => obj[key] = response[i][index]); result.push(obj); } return result; } var input = [ ["POP", "GEONAME", "state"], ["4863300", "Alabama", "01"], ["741894", "Alaska", "02"], ["6931071", "Arizona", "04"], ["2988248", "Arkansas", "05"], ["39250017", "California", "06"] ]; console.log(modifyArrayResponse(input));
也许像这样
var oData = [
[
"POP",
"GEONAME",
"state"
],
[
"4863300",
"Alabama",
"01"
],
[
"741894",
"Alaska",
"02"
],
[
"6931071",
"Arizona",
"04"
],
[
"2988248",
"Arkansas",
"05"
],
[
"39250017",
"California",
"06"
]
];
var oResult = [];
var columns = null;
oData.forEach(function(data) {
if (columns === null) {
columns = data;
} else {
var oEntry = {};
oResult.push(oEntry);
data.forEach(function(d, i) {
oEntry[columns[i]] = d;
});
}
});
console.log(oResult);
使用shift()
获取和删除列名称,然后map()
数组的其余部分,并在列上使用reduce()
创建每个对象
let cols = data.shift() let res = data.map(arr => cols.reduce((a, c, i) => (a[c] = arr[i], a), {})) console.log(res)
<script> var data = [ ["POP", "GEONAME", "state"], ["4863300", "Alabama", "01"], ["741894", "Alaska", "02"], ["6931071", "Arizona", "04"], ["2988248", "Arkansas", "05"], ["39250017", "California", "06"] ] </script>
可以单独的键和值成单独的阵列,然后遍历与所述值Array.prototype.map
和构造与该对象Array.prototype.reduce
:
const data=[["POP","GEONAME","state"],["4863300","Alabama","01"],["741894","Alaska","02"],["6931071","Arizona","04"],["2988248","Arkansas","05"],["39250017","California","06"]]; const [keys, ...vals] = data; const result = vals.map(item => item.reduce((all,v,i) => (all[keys[i]] = item[i], all), {})); console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.