假设我有一个这样的列表结构:
[
"id1", "01-01-2019", "name1", "€ 5,60",
"id2", "02-01-2019", "name2", "€ 5,70",
"id3", "03-01-2019", "name3", "€ 5,20",
...
]
,我想将其转换为带有json对象的列表,如下所示:
[
{
"id": "id1",
"date": "01-01-2019",
"name": "name1",
"value": "€ 5,60"
},
{
"id": "id2",
"date": "02-01-2019",
"name": "name2",
"value": "€ 5,70"
},
{
"id": "id3",
"date": "03-01-2019",
"name": "name3",
"value": "€ 5,20"
},
...
]
在JavaScript中最有效的方法是什么?
答案 0 :(得分:5)
我将使用带有预先声明的简单while
循环:
console.assert(data.length % 4 === 0); // Or whatever you use for assertions
const result = [];
let i = 0;
while (i < data.length) {
result.push({
id: data[i++],
date: data[i++],
name: data[i++],
value: data[i++]
});
}
实时示例:
const data = [
"id1", "01-01-2019", "name1", "€ 5,60",
"id2", "02-01-2019", "name2", "€ 5,70",
"id3", "03-01-2019", "name3", "€ 5,20"
];
console.assert(data.length % 4 === 0);
const result = [];
let i = 0;
while (i < data.length) {
result.push({
id: data[i++],
date: data[i++],
name: data[i++],
value: data[i++]
});
}
console.log(result);
.as-console-wrapper {
max-height: 100% !important;
}
答案 1 :(得分:2)
我认为这种方法将是最有效的,因为循环运行了1/4次。如果您还有其他建议,请随时提出建议。
const array = [
"id1", "01-01-2019", "name1", "€ 5,60",
"id2", "02-01-2019", "name2", "€ 5,70",
"id3", "03-01-2019", "name3", "€ 5,20",
'id4', '03-01-2019'
];
const finalArray = [];
for (let i=0; i < array.length; i += 4) {
const obj = {};
obj.id = array[i];
obj.data = array[i + 1] ? array[i + 1] : 'NA'; // This will check the index is available or not.
obj.name = array[i + 2] ? array[i + 2] : 'NA';
obj.value =array[i + 3] ? array[i + 3] : 'NA';
finalArray.push(obj);
}
console.log('finalArray = ', finalArray);
答案 2 :(得分:2)
您可以使用Array.reduce()
进行无循环操作,方法是在输入数据上每4次迭代将一个新项推送到输出中:
const data = [
"id1", "01-01-2019", "name1", "€ 5,60",
"id2", "02-01-2019", "name2", "€ 5,70",
"id3", "03-01-2019", "name3", "€ 5,20",
];
const result = data.reduce((acc, _, i, arr) => {
if (i % 4 === 0) {
acc.push({ id: arr[i], date: arr[i+1], name: arr[i+2], value: arr[i+3] });
}
return acc;
}, []);
console.log(result);
答案 3 :(得分:2)
您可以对数组的各个部分进行分解,并构建一个用于推送的新对象。
const makeObject = ([id, date, name, value]) => ({ id, date, name, value });
var data = ["id1", "01-01-2019", "name1", "€ 5,60", "id2", "02-01-2019", "name2", "€ 5,70", "id3", "03-01-2019", "name3", "€ 5,20"],
i = 0,
result = [];
while (i < data.length) {
result.push(makeObject(data.slice(i, i += 4)));
}
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 4 :(得分:1)
将数组划分为子数组,然后将这些数组映射到对象:
const arr = [
"id1", "01-01-2019", "name1", "€ 5,60",
"id2", "02-01-2019", "name2", "€ 5,70",
"id3", "03-01-2019", "name3", "€ 5,20"
]
const result = chunk(arr, 4).map(item => ({ id: item[0], date: item[1], name: item[2], cost: item[3]}))
console.log(result)
function chunk(arr, size) {
const result = []
for (let i = 0; i < arr.length; i += size)
result.push(arr.slice(i, i + size))
return result
}
答案 5 :(得分:1)
您可以使用Array.from
做某事。此处,密钥未在循环中进行硬编码。只要您具有有效的输入数组数据,此方法就可以任意数量的预定义 keys
。
const array = [
"id1", "01-01-2019", "name1", "€ 5,60",
"id2", "02-01-2019", "name2", "€ 5,70",
"id3", "03-01-2019", "name3", "€ 5,20",
]
const keys = ["id", "date", "name", "value"],
length = array.length / keys.length;
const output = Array.from({ length }, (_, i) =>
keys.reduce((r,k,j) => ({ ...r, [k]: array[i * keys.length + j] }), {})
);
console.log(output)