node.js 拆分由“=”符号分隔的管道分隔键值对
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16249610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split a pipe delimited key-value pair separated by '=' symbol
提问by Amol M Kulkarni
We are receiving an input parameter value as a pipe-delimited key-value pair, separated with =symbols. For example:
我们以管道分隔的键值对的形式接收输入参数值,用=符号分隔。例如:
"|User=0101|Name=ImNewUser|IsAdmin=0|RefId=23ae2123cd223bf235|"
So the format is: |KEY=VALUE|KEY_2=VALUE_2|....|KEY_n=VALUE_n|
所以格式是: |KEY=VALUE|KEY_2=VALUE_2|....|KEY_n=VALUE_n|
I need to split it into a JSON object. So, my object should be :
我需要将它拆分为一个 JSON 对象。所以,我的对象应该是:
{
'User':'0101',
'Name':'ImNewUser',
'IsAdmin':'0',
'RefId'='23ae2123cd223bf235'
}
What will be best way to go, since there are multiple options:
最好的方法是什么,因为有多种选择:
- I can use split with
|and again on each element split with=. - I can depend on regular expression and do string replace.
- Split it with
=remove trailing|symbol and associate two different arrays with indexes.
- 我可以
|在每个元素上使用 split with并再次使用split with=。 - 我可以依赖正则表达式并进行字符串替换。
- 使用
=删除尾随|符号拆分它并将两个不同的数组与索引相关联。
Can anyone tell me the best/most efficient way of doing this in JavaScript (programming in Node.js)?
谁能告诉我在 JavaScript 中执行此操作的最佳/最有效方法(在 Node.js 中编程)?
回答by Paul
The first one sounds good:
第一个听起来不错:
var str = "|User=0101|Name=ImNewUser|IsAdmin=0|RefId=23ae2123cd223bf235|";
var result = {};
str.split('|').forEach(function(x){
var arr = x.split('=');
arr[1] && (result[arr[0]] = arr[1]);
});
回答by Billy Moon
回答by dpren
Without mutation
无突变
You don't need the outer pipes. If necessary, trim them off str.slice(1, str.length - 1)
你不需要外管。如有必要,修剪它们str.slice(1, str.length - 1)
const str = "User=0101|Name=ImNewUser|IsAdmin=0|RefId=23ae2123cd223bf235";
str.split('|').reduce((accum, x) => {
const kv = x.split('=');
return {...accum, ...{[kv[0]]: kv[1]}};
}, {})
回答by Allen Sarkisyan
Cleanest way possible, you can modify the source to split by a different delimiter.
尽可能干净的方式,您可以修改源以使用不同的分隔符进行拆分。
https://gist.github.com/allensarkisyan/5873977#file-parsequerystring-js
https://gist.github.com/allensarkisyan/5873977#file-parsequerystring-js
`/**
* @name - parseQueryString
* @author - Allen Sarkisyan
* @license - Open Source MIT License
*
* @description - Parses a query string into an Object.
* - Optionally can also parse location.search by invoking without an argument
*/`
`
function parseQueryString(queryString) {
var obj = {};
function sliceUp(x) { x.replace('?', '').split('&').forEach(splitUp); }
function splitUp(x) { var str = x.split('='); obj[str[0]] = decodeURIComponent(str[1]); }
try { (!queryString ? sliceUp(location.search) : sliceUp(queryString)); } catch(e) {}
return obj;
}
`
回答by Yan Foto
I would just use regular expressions to group (see here) each KEY=VALUEpair and then iterate over them to fill up the JSON object. So you could have something like this:
我只会使用正则表达式对每一对进行分组(参见此处)KEY=VALUE,然后遍历它们以填充 JSON 对象。所以你可以有这样的事情:
var re = /(([^=\|]+)=([^=\|]+))/g;
var match;
var myString = "|User=0101|Name=ImNewUser|IsAdmin=0|RefId=23ae2123cd223bf235|";
while (match = re.exec(myString)) {
console.log(match);
// first iteration returns ["User=0101","User=0101","User","0101"] and so on
}
回答by Yury Tarabanko
var str = "|User=0101|Name=ImNewUser|IsAdmin=0|RefId=23ae2123cd223bf235|";
var result = {}, name;
str.substring(1, str.length-1).split(/\||=/).forEach(function(item, idx){
idx%2 ? (result[name] = item) : (name = item);
});


