我想从此文本中删除所有空格,然后将结果整理成对象
var notOrganizedObj = {
"06fe562bc26ad23ec2a717c893ccc4407297de136b2761a035e80eea75637d3c-0" : " ENABLED 10035 mYkSVf9AcAMCScw91kcPBiKawe51LYzJRP 8.9.30.2:19662 1557196699 4242543 1557188820",
"0282ab652d149e092051df77db70187efe5d3e61ed714a9a4efa69d0cc4452a5-0" : " ENABLED 10035 mMni8ALvkdUHN3jxgHnhJ99S2VthctFbM4 8.9.30.12:19662 1557196829 3169507 1557194872",
}
我已经尝试过
Object.keys(masternodelist).map(function(key, index) {
var list = masternodelist[key].split(' ', '')
console.log(masternodelist[key])
});
所以我可以拆分键的值,然后将其拆分为对象中的自定义键
预期结果将是这样
[{
"address": "94.177.163.40:4836",
"tx": "06fe562bc26ad23ec2a717c893ccc4407297de136b2761a035e80eea75637d3c",
"payee": "WNijK2poAXXoEPp87YT1paWBGsTJYaQhcL",
"status": "ENABLED",
"protocol": 70210,
"daemonversion": "0.12.3.3",
"sentinelversion": "1.1.0",
"sentinelstate": "current",
"lastseen": 1557198027,
"activeseconds": 4705452,
"lastpaidtime": 1557181730,
"lastpaidblock": 184934
},
{
"address": "95.179.229.205:4836",
"tx": "0282ab652d149e092051df77db70187efe5d3e61ed714a9a4efa69d0cc4452a5",
"payee": "WcpfARQfyE4SfzLacehgoRrjxQg3wK3YBY",
"status": "ENABLED",
"protocol": 70210,
"daemonversion": "0.12.3.3",
"sentinelversion": "1.1.0",
"sentinelstate": "current",
"lastseen": 1557198094,
"activeseconds": 8397933,
"lastpaidtime": 1556925620,
"lastpaidblock": 180869
}]
答案 0 :(得分:1)
说实话,由于许多键/属性缺失,我无法给您一个完整的答案,而且我不知道如何获取其余的值。
但是基本上,我建议您遍历该对象,将字符串拆分为字符串数组(.filter(i => i)
删除数组中的空字符串”,然后将它们映射到所需的结果中。 / p>
我建议您这样做:
const notOrganizedObj = {
"06fe562bc26ad23ec2a717c893ccc4407297de136b2761a035e80eea75637d3c-0": " ENABLED 10035 mYkSVf9AcAMCScw91kcPBiKawe51LYzJRP 8.9.30.2:19662 1557196699 4242543 1557188820",
"0282ab652d149e092051df77db70187efe5d3e61ed714a9a4efa69d0cc4452a5-0": " ENABLED 10035 mMni8ALvkdUHN3jxgHnhJ99S2VthctFbM4 8.9.30.12:19662 1557196829 3169507 1557194872",
}
const res = [];
for (let key in notOrganizedObj) {
const wordList = notOrganizedObj[key].split(' ').filter(i => i);
res.push({
address: wordList[3],
tx: key.split('-')[0],
payee: '',
status: wordList[1],
protocol: '',
daemonversion: '',
sentinelversion: '',
sentinelstate: '',
lastseen: '',
activeseconds: '',
lastpaidtime: '',
lastpaidblock: ''
})
}
console.log(res);
答案 1 :(得分:1)
我将创建一个映射数组,该数组按您希望它们在字符串中出现的顺序列出这些键。
在map
回调中,您可以使用Array.reduce()
来配对键和值。
const mapToKeys = (obj, keys) => {
return Object.keys(obj).map(key => { //for each key in the input object
let values = obj[key].match(/\S+/g) || []; //split value by whitespace
return values.reduce((o,v,i) => ({...o, [keys[i]]: v}), {tx:key}); //zip key-value pairs
});
};
const input = {"06fe562bc26ad23ec2a717c893ccc4407297de136b2761a035e80eea75637d3c-0" : " ENABLED 10035 mYkSVf9AcAMCScw91kcPBiKawe51LYzJRP 8.9.30.2:19662 1557196699 4242543 1557188820", "0282ab652d149e092051df77db70187efe5d3e61ed714a9a4efa69d0cc4452a5-0" : " ENABLED 10035 mMni8ALvkdUHN3jxgHnhJ99S2VthctFbM4 8.9.30.12:19662 1557196829 3169507 1557194872"}
const keys = ["status", "protocol", "payee", "address", "lastseen", "activeseconds", "lastpaidtime"];
console.log( mapToKeys(input,keys) );
答案 2 :(得分:0)
您可以forEach
对象键和split
值,然后push
每个具有结果属性的数组项映射。
var notOrganizedObj = {
"06fe562bc26ad23ec2a717c893ccc4407297de136b2761a035e80eea75637d3c-0" : " ENABLED 10035 mYkSVf9AcAMCScw91kcPBiKawe51LYzJRP 8.9.30.2:19662 1557196699 4242543 1557188820",
"0282ab652d149e092051df77db70187efe5d3e61ed714a9a4efa69d0cc4452a5-0" : " ENABLED 10035 mMni8ALvkdUHN3jxgHnhJ99S2VthctFbM4 8.9.30.12:19662 1557196829 3169507 1557194872",
}
let result = [];
Object.keys(notOrganizedObj).forEach( c=> {
var list = notOrganizedObj[c].split(' ').filter(item=>item != null && item != "");
//console.log(list)
result.push({
"address": list[3],
"tx": c,
"payee": list[2],
"status": list[0],
"protocol": list[1],
"daemonversion": "0.12.3.3",
"sentinelversion": "1.1.0",
"sentinelstate": "current",
"lastseen": 1557198027,
"activeseconds": 4705452,
"lastpaidtime": list[6],
"lastpaidblock": 184934
})
});
console.log(result);
var notOrganizedObj = {
"06fe562bc26ad23ec2a717c893ccc4407297de136b2761a035e80eea75637d3c-0" : " ENABLED 10035 mYkSVf9AcAMCScw91kcPBiKawe51LYzJRP 8.9.30.2:19662 1557196699 4242543 1557188820",
"0282ab652d149e092051df77db70187efe5d3e61ed714a9a4efa69d0cc4452a5-0" : " ENABLED 10035 mMni8ALvkdUHN3jxgHnhJ99S2VthctFbM4 8.9.30.12:19662 1557196829 3169507 1557194872",
}
let result = [];
Object.keys(notOrganizedObj).forEach( c=> {
var list = notOrganizedObj[c].split(' ').filter(item=>item != null && item != "");
//console.log(list)
result.push({
"address": list[3],
"tx": c,
"payee": list[2],
"status": list[0],
"protocol": list[1],
"daemonversion": "0.12.3.3",
"sentinelversion": "1.1.0",
"sentinelstate": "current",
"lastseen": 1557198027,
"activeseconds": 4705452,
"lastpaidtime": list[6],
"lastpaidblock": 184934
})
});
console.log(result);