javascript - Convert string to object in NodeJS - Stack Overflow

I have a string in this format:var x = "a=1; b=2; c=3; d=4"and I would like to convert it to

I have a string in this format:

var x = "a=1; b=2; c=3; d=4"

and I would like to convert it to an object like this:

var y = {
    a: "1",
    b: "2",
    c: "3",
    d: "4"
    }

Any ideas how to achieve that?

I have a string in this format:

var x = "a=1; b=2; c=3; d=4"

and I would like to convert it to an object like this:

var y = {
    a: "1",
    b: "2",
    c: "3",
    d: "4"
    }

Any ideas how to achieve that?

Share Improve this question edited Sep 10, 2015 at 16:22 Felix Kling 818k181 gold badges1.1k silver badges1.2k bronze badges asked Sep 10, 2015 at 16:04 SoufiaaneSoufiaane 2,1177 gold badges26 silver badges45 bronze badges 6
  • So show us what you have tried so far – Vsevolod Goloviznin Commented Sep 10, 2015 at 16:11
  • 2 i tried x.split(";") wich give me ["a=1", "b=2", "c=3", "d=4"]. and i'm thinking to do another split by "=" for each item in the table... ? – Soufiaane Commented Sep 10, 2015 at 16:17
  • 2 And what's wrong with the solution you're talking about? – Vsevolod Goloviznin Commented Sep 10, 2015 at 16:17
  • 1 benalman./news/2010/03/theres-no-such-thing-as-a-json. You want to create a JavaScript object, not JSON. Either way, simply split the string (multiple times) and create a new object from the parts. What exactly are you having problems with? – Felix Kling Commented Sep 10, 2015 at 16:21
  • actually, what i'm trying to do is to parse a mail header. here is the function: mailListener.on("mail", function(mail, seqno, attributes){ var dkim = mail.headers["dkim-signature"].split(";") // ..... } since i'm running that in nodejs(asynchronously), it gets messed up as i try to boocle over the table... – Soufiaane Commented Sep 10, 2015 at 16:29
 |  Show 1 more ment

3 Answers 3

Reset to default 5

This works in iE9+

var x = "a=1; b=2; c=3; d=4",
    y = {};

x.split(';').map(function (i) {
  return i.split('=')
}).forEach(function (j) {
  y[j[0].trim()] = j[1]
});

If you are using Node.js v4+

let x = "a=1; b=2; c=3; d=4",
    y = {}

x.split(';').map(i => i.split('=')).forEach(j => y[j[0].trim()] = j[1])

You could try this (not bullet proof, refer to ments):

var json, str;
str = 'a=1; b=2; c=3; d=4';
str = str.replace(/\s*;\s*/g, ',');
str = str.replace(/([^,]+)=([^,]+)/g, '"$1":"$2"');
str = '{' + str + '}';
json = JSON.parse(str);

document.write(
  '<pre>' + JSON.stringify(json) + '</pre>'
);

here is what i did and it seems to work fine:

var y = x.split(";");
var obj = {};
for(var i = 0; i < y.length ; i++){
    var k = y[i].split("=");
    var r = k[0].replace(" ", "");
    obj[r] = k[1];
}
console.log(obj);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745231523a4617697.html

相关推荐

  • javascript - Convert string to object in NodeJS - Stack Overflow

    I have a string in this format:var x = "a=1; b=2; c=3; d=4"and I would like to convert it to

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信