gpt4 book ai didi

javascript - 使用 JavaScript 将字符串转换为对象

转载 作者:行者123 更新时间:2023-11-28 04:47:38 25 4
gpt4 key购买 nike

我想知道在 JavaScript 中将此字符串转换为对象的最有效方法。

这是丑陋的字符串:

  "\"date\",\"initRegistrations\",\"registrations\",\"finalizeRegistrations\"\r\n\"03/29/2017\",\"0\",\"0\",\"0\"\r\n\"03/30/2017\",\"5\",\"4\",\"4\"\r\n\"03/31/2017\",\"42\",\"0\",\"0\"\r\n\"04/01/2017\",\"192\",\"0\",\"0\"\r\n\"04/02/2017\",\"4\",\"0\",\"0\"\r\n\"04/03/2017\",\"23\",\"0\",\"0\"\r\n\"04/04/2017\",\"5\",\"0\",\"0\"\r\n"

目前我正在使用这个函数,但它不能正常工作,预期的输出将是一个带有日期、initRegistrations、registrations、finalizeRegistrations键的对象的对象:

   function convertToArrayOfObjects(data) {
var keys = data.shift(),
i = 0, k = 0,
obj = null,
output = [];

for (i = 0; i < data.length; i++) {
obj = {};

for (k = 0; k < keys.length; k++) {
if(data[i][k]){
obj[keys[k]] = data[i][k];
}

}

output.push(obj);
}

return output;
}

有没有什么奇特的方法可以轻松实现这一点,或者我应该去玩我的对象数组?任何帮助或建议表示赞赏。谢谢。

最佳答案

对于某些人来说可能太实用了,但这就是我的编写方式。

const s =   "\"date\",\"initRegistrations\",\"registrations\",\"finalizeRegistrations\"\r\n\"03/29/2017\",\"0\",\"0\",\"0\"\r\n\"03/30/2017\",\"5\",\"4\",\"4\"\r\n\"03/31/2017\",\"42\",\"0\",\"0\"\r\n\"04/01/2017\",\"192\",\"0\",\"0\"\r\n\"04/02/2017\",\"4\",\"0\",\"0\"\r\n\"04/03/2017\",\"23\",\"0\",\"0\"\r\n\"04/04/2017\",\"5\",\"0\",\"0\"\r\n";

// Remove surrounding quotes from a string.
const removeQuotes = s => s.replace(/^"|"$/g, '');

// Split input by newline ('\r\n')
const lines = s.trim().split('\r\n');

// Get header (first line of input) and remove quotes.
const header = lines[0].split(',').map(removeQuotes)

// The rest of the string is data. Split by comma and remove quotes.
const data = lines.splice(1).map(line => line.split(',').map(removeQuotes));

// Given header row and data row, combine into an object.
const zipWith = header => row => {
return header.reduce((map, item, index) => {
map[item] = row[index];
return map;
}, {});
}

// Now process all your data with a map :)
const result = data.map(zipWith(header));

console.log(result);

很确定输出正是您想要的:

[
{
"date": "03/29/2017",
"initRegistrations": "0",
"registrations": "0",
"finalizeRegistrations": "0"
},
{
"date": "03/30/2017",
"initRegistrations": "5",
"registrations": "4",
"finalizeRegistrations": "4"
},
{
"date": "03/31/2017",
"initRegistrations": "42",
"registrations": "0",
"finalizeRegistrations": "0"
},
{
"date": "04/01/2017",
"initRegistrations": "192",
"registrations": "0",
"finalizeRegistrations": "0"
},
{
"date": "04/02/2017",
"initRegistrations": "4",
"registrations": "0",
"finalizeRegistrations": "0"
},
{
"date": "04/03/2017",
"initRegistrations": "23",
"registrations": "0",
"finalizeRegistrations": "0"
},
{
"date": "04/04/2017",
"initRegistrations": "5",
"registrations": "0",
"finalizeRegistrations": "0"
}
]

关于javascript - 使用 JavaScript 将字符串转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43218496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com