简体   繁体   English

如何将包含空格的字符串转换为有组织的对象

[英]how to convert a string that contains spaces to organized object

I want to remove all spaces from this text then organize the result into object 我想从此文本中删除所有空格,然后将结果整理成对象

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",
}

I've tried this 我已经试过了

Object.keys(masternodelist).map(function(key, index) {
 var list = masternodelist[key].split(' ', '')
  console.log(masternodelist[key])
});

so i can split the value of keys then some how added to custom key in my object 所以我可以拆分键的值,然后将其添加到对象的自定义键中

the expected result is to be something like this 预期的结果是像这样

[{
    "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
  }]

TO be honest, I can't give you a complete answer, as many of the key/properties are missing, and I do not know how to get the rest of the values. 老实说,由于许多键/属性丢失了,我无法给您一个完整的答案,而且我不知道如何获取其余的值。

But basically, I recommend you to iterate through the object, split the string into an array of strings ( .filter(i => i) removes the empty strings '' within the array), and then map them into your desired result. 但基本上,我建议您遍历对象,将字符串拆分为字符串数组( .filter(i => i)删除数组中的空字符串”,然后将其映射为所需的结果。

I would recommend you to do this: 我建议您这样做:

 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); 

You can forEach Object key and split value then push each array item map with result property. 您可以forEach Object键和split值,然后使用result属性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); 

I'd create a mapping array that lists the keys in the order you expect them to appear in the string. 我将创建一个映射数组,该数组按您希望它们在字符串中出现的顺序列出这些键。

Within your map callback you could use Array.reduce() to pair the keys and values. 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) ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM