gpt4 book ai didi

javascript - 在node js express中将字符串解析为json

转载 作者:太空宇宙 更新时间:2023-11-04 02:48:14 25 4
gpt4 key购买 nike

所以,我使用 python-node jsexpress 在浏览器中显示了一些字符串数据。数据是这样的:

some_logs abcdata_start,ds0,06-01-2016,3,4,5,06-01-2015,8,9,10,05-01-2015,10,11,12,ds1,06-01-2016,32,42,52,06-01-2015,8,9,10,05-01-20 15,10,11,12,abcdata_end,some_logs,abcdata_start,ds0,06-01-2016,13,14,15,06-01-2015,18,19,10,05-01-2015,10,11,12,ds1,06-01-2016,33,43,53,06-01- 2015,8,9,10,05-01-2015,10,11,12,abcdata_end

我想将此数据转换为 json,这样:

"intervals": {
"06-01-2016": {
"ds0": {
"0": [
3, 4, 5
],
"1": [
13,14,15
]
},
"ds1": {
"0": [
32, 42, 52
],
"1": [
33, 43, 53
]
}
},
"06-01-2015": {
filling values in similar way as above
}
}

最佳答案

我在这个问题上花了一些时间,我不确定我的解决方案是否最佳,但它有效:

var str = 'some_logs abcdata_start,ds0,06-01-2016,3,4,5,06-01-2015,8,9,10,05-01-2015,10,11,12,ds1,06-01-2016,3,4,5,06-01-2015,8,9,10,05-01-2015,10,11,12,abcdata_end,some_logs,abcdata_start,ds0,06-01-2016,13,14,15,06-01-2015,18,19,10,05-01-2015,10,11,12,ds1,06-01-2016,3,4,5,06-01-2015,8,9,10,05-01-2015,10,11,12,abcdata_end';
var indexes = [];
var ds = {};

str.split(',').forEach((el, i) => {
if (el.includes("start") || el.includes("end"))
indexes.push(i);
});

indexes = indexes.reduce((acc, el, i, arr) => {
if (i % 2 == 0)
acc.push([el, arr[i+1]]);
return acc;
}, []);

indexes.forEach(arr => {
var tmp = str.split(',').slice(arr[0] + 1, arr[1]);
var tmpIndex = "";
tmp.forEach(val => {
if (val.length === 3 && val.includes("ds"))
tmpIndex = val;
else
ds[tmpIndex]
? ds[tmpIndex].push(val)
: ds[tmpIndex] = [val];
});
});

Object.keys(ds).forEach(key => {
var tmpIndex = "";
ds[key] = ds[key].reduce((acc, val) => {
if (val.match(/[0-9]{2}-[0-9]{2}-[0-9]{4}/) !== null)
tmpIndex = val;
else
acc[tmpIndex]
? acc[tmpIndex].push(val)
: acc[tmpIndex] = [val]
return acc;
}, {});


Object.keys(ds[key]).forEach(dateKey => {
ds[key][dateKey] = ds[key][dateKey].reduce((acc, val, i, arr) => {
if (i % 3 == 0)
acc.push([val, arr[i+1], arr[i+2]]);
return acc;
}, [])
});
});

var res = {};
Object.keys(ds).forEach(dsNum => {
Object.keys(ds[dsNum]).forEach(date => {
if (res.hasOwnProperty(date)) {
res[date][dsNum]
? res[date][dsNum].push(ds[dsNum][date])
: res[date][dsNum] = [ds[dsNum][date]];
} else {
res[date] = {[dsNum]: [ds[dsNum][date]]}
}
})
})

Object.keys(res).forEach(date => {
Object.keys(res[date]).forEach(ds => {
res[date][ds] = res[date][ds][0];
})
})

编辑

简短版本,相同,但有很多丑陋的东西、技巧和嵌套三元:

var str = 'some_logs abcdata_start,ds0,06-01-2016,3,4,5,06-01-2015,8,9,10,05-01-2015,10,11,12,ds1,06-01-2016,32,42,52,06-01-2015,8,9,10,05-01-2015,10,11,12,abcdata_end,some_logs,abcdata_start,ds0,06-01-2016,13,14,15,06-01-2015,18,19,10,05-01-2015,10,11,12,ds1,06-01-2016,33,43,53,06-01-2015,8,9,10,05-01-2015,10,11,12,abcdata_end', indexes = [], ds = {}, res = {};

str.split(',').forEach((el, i) => (el.includes("start") || el.includes("end")) ? indexes.push(i) : null);
indexes = indexes.reduce((acc, el, i, arr) => (i % 2 == 0) ? acc.concat([[el, arr[i+1]]]) : acc, []);
indexes.forEach((arr, tmpIndex = "") => str.split(',').slice(arr[0] + 1, arr[1]).forEach(val => (val.length === 3 && val.includes("ds")) ? tmpIndex = val : ds[tmpIndex] ? ds[tmpIndex].push(val) : ds[tmpIndex] = [val]));

Object.keys(ds).forEach((key, tmpIndex = "") => {
ds[key] = ds[key].reduce((acc, val) => ((val.match(/[0-9]{2}-[0-9]{2}-[0-9]{4}/) !== null) ? tmpIndex = val : acc[tmpIndex] ? acc[tmpIndex].push(val) : acc[tmpIndex] = [val]) == undefined ? acc : acc, {});
Object.keys(ds[key]).forEach(dateKey => ds[key][dateKey] = ds[key][dateKey].reduce((acc, val, i, arr) => (i % 3 == 0) ? acc.concat([[val, arr[i+1], arr[i+2]]]) : acc, []));
});
Object.keys(ds).forEach(dsNum => Object.keys(ds[dsNum]).forEach(date => (res.hasOwnProperty(date)) ? res[date][dsNum] ? res[date][dsNum].push(ds[dsNum][date]) : res[date][dsNum] = [ds[dsNum][date]] : res[date] = {[dsNum]: [ds[dsNum][date]]}));
Object.keys(res).forEach(date => Object.keys(res[date]).forEach(ds => res[date][ds] = res[date][ds][0]));

希望有帮助,
谨致问候,

关于javascript - 在node js express中将字符串解析为json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44515077/

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