Convert a multi-line string into a javascript object The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceHow to validate an email address in JavaScript?How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?How do I make the first letter of a string uppercase in JavaScript?How to replace all occurrences of a string in JavaScriptWhat does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?Storing Objects in HTML5 localStorageHow do I remove a particular element from an array in JavaScript?
What was the last x86 CPU that did not have the x87 floating-point unit built in?
How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time
Loose spokes after only a few rides
The following signatures were invalid: EXPKEYSIG 1397BC53640DB551
Am I ethically obligated to go into work on an off day if the reason is sudden?
Is 'stolen' appropriate word?
Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?
Using dividends to reduce short term capital gains?
Circular reasoning in L'Hopital's rule
different output for groups and groups USERNAME after adding a username to a group
How do you keep chess fun when your opponent constantly beats you?
Example of compact Riemannian manifold with only one geodesic.
Did the new image of black hole confirm the general theory of relativity?
Sort list of array linked objects by keys and values
Can each chord in a progression create its own key?
Did the UK government pay "millions and millions of dollars" to try to snag Julian Assange?
Keeping a retro style to sci-fi spaceships?
"... to apply for a visa" or "... and applied for a visa"?
Single author papers against my advisor's will?
How to determine omitted units in a publication
Is this wall load bearing? Blueprints and photos attached
My body leaves; my core can stay
What happens to a Warlock's expended Spell Slots when they gain a Level?
Would an alien lifeform be able to achieve space travel if lacking in vision?
Convert a multi-line string into a javascript object
The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceHow to validate an email address in JavaScript?How do JavaScript closures work?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?How do I make the first letter of a string uppercase in JavaScript?How to replace all occurrences of a string in JavaScriptWhat does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?Storing Objects in HTML5 localStorageHow do I remove a particular element from an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I've my raw data that looks like this:
Last Name, First Name (Details-Details) #ID
Last Name, First Name (Details-Details) #ID
Last Name, First Name (Details-Details) #ID
Last Name, First Name (Details-Details) #ID
Last Name, First Name (Details-Details) #ID
Last Name, First Name (Details-Details) #ID
x1000
I'd like to convert into a loopable object with keys and values something like:
var d =
"FirstName LastName": "#ID";
"FirstName LastName": "#ID";
"FirstName LastName": "#ID";
"FirstName LastName": "#ID";
"FirstName LastName": "#ID";
"FirstName LastName": "#ID";
;
How can convert this to a javascript object? I'm thinking stringify
may be a good starting point. FirstName
have different values. They are just for the purpose of demonstration
javascript regex
|
show 4 more comments
I've my raw data that looks like this:
Last Name, First Name (Details-Details) #ID
Last Name, First Name (Details-Details) #ID
Last Name, First Name (Details-Details) #ID
Last Name, First Name (Details-Details) #ID
Last Name, First Name (Details-Details) #ID
Last Name, First Name (Details-Details) #ID
x1000
I'd like to convert into a loopable object with keys and values something like:
var d =
"FirstName LastName": "#ID";
"FirstName LastName": "#ID";
"FirstName LastName": "#ID";
"FirstName LastName": "#ID";
"FirstName LastName": "#ID";
"FirstName LastName": "#ID";
;
How can convert this to a javascript object? I'm thinking stringify
may be a good starting point. FirstName
have different values. They are just for the purpose of demonstration
javascript regex
1
Use regex to extract data
– Pranav C Balan
Mar 22 at 5:39
maybe use regex to extract data and convert it to object like you want.
– vibhor1997a
Mar 22 at 5:39
yea it's not JSON. above is it's format when pasted in too notepad
– EthanPrisonMike
Mar 22 at 5:40
4
Please post the actual data. You cannot have duplicate keys in an object. AreFirst Name
etc just placeholders?
– adiga
Mar 22 at 5:40
1
@EthanPrisonMike please use better sample data in your question, something that actually makes sense as a JS object. What is(Details-Details)
? Is it literally that or something else?
– Phil
Mar 22 at 5:42
|
show 4 more comments
I've my raw data that looks like this:
Last Name, First Name (Details-Details) #ID
Last Name, First Name (Details-Details) #ID
Last Name, First Name (Details-Details) #ID
Last Name, First Name (Details-Details) #ID
Last Name, First Name (Details-Details) #ID
Last Name, First Name (Details-Details) #ID
x1000
I'd like to convert into a loopable object with keys and values something like:
var d =
"FirstName LastName": "#ID";
"FirstName LastName": "#ID";
"FirstName LastName": "#ID";
"FirstName LastName": "#ID";
"FirstName LastName": "#ID";
"FirstName LastName": "#ID";
;
How can convert this to a javascript object? I'm thinking stringify
may be a good starting point. FirstName
have different values. They are just for the purpose of demonstration
javascript regex
I've my raw data that looks like this:
Last Name, First Name (Details-Details) #ID
Last Name, First Name (Details-Details) #ID
Last Name, First Name (Details-Details) #ID
Last Name, First Name (Details-Details) #ID
Last Name, First Name (Details-Details) #ID
Last Name, First Name (Details-Details) #ID
x1000
I'd like to convert into a loopable object with keys and values something like:
var d =
"FirstName LastName": "#ID";
"FirstName LastName": "#ID";
"FirstName LastName": "#ID";
"FirstName LastName": "#ID";
"FirstName LastName": "#ID";
"FirstName LastName": "#ID";
;
How can convert this to a javascript object? I'm thinking stringify
may be a good starting point. FirstName
have different values. They are just for the purpose of demonstration
javascript regex
javascript regex
edited Mar 25 at 18:00
adiga
12.4k62745
12.4k62745
asked Mar 22 at 5:36
EthanPrisonMikeEthanPrisonMike
136
136
1
Use regex to extract data
– Pranav C Balan
Mar 22 at 5:39
maybe use regex to extract data and convert it to object like you want.
– vibhor1997a
Mar 22 at 5:39
yea it's not JSON. above is it's format when pasted in too notepad
– EthanPrisonMike
Mar 22 at 5:40
4
Please post the actual data. You cannot have duplicate keys in an object. AreFirst Name
etc just placeholders?
– adiga
Mar 22 at 5:40
1
@EthanPrisonMike please use better sample data in your question, something that actually makes sense as a JS object. What is(Details-Details)
? Is it literally that or something else?
– Phil
Mar 22 at 5:42
|
show 4 more comments
1
Use regex to extract data
– Pranav C Balan
Mar 22 at 5:39
maybe use regex to extract data and convert it to object like you want.
– vibhor1997a
Mar 22 at 5:39
yea it's not JSON. above is it's format when pasted in too notepad
– EthanPrisonMike
Mar 22 at 5:40
4
Please post the actual data. You cannot have duplicate keys in an object. AreFirst Name
etc just placeholders?
– adiga
Mar 22 at 5:40
1
@EthanPrisonMike please use better sample data in your question, something that actually makes sense as a JS object. What is(Details-Details)
? Is it literally that or something else?
– Phil
Mar 22 at 5:42
1
1
Use regex to extract data
– Pranav C Balan
Mar 22 at 5:39
Use regex to extract data
– Pranav C Balan
Mar 22 at 5:39
maybe use regex to extract data and convert it to object like you want.
– vibhor1997a
Mar 22 at 5:39
maybe use regex to extract data and convert it to object like you want.
– vibhor1997a
Mar 22 at 5:39
yea it's not JSON. above is it's format when pasted in too notepad
– EthanPrisonMike
Mar 22 at 5:40
yea it's not JSON. above is it's format when pasted in too notepad
– EthanPrisonMike
Mar 22 at 5:40
4
4
Please post the actual data. You cannot have duplicate keys in an object. Are
First Name
etc just placeholders?– adiga
Mar 22 at 5:40
Please post the actual data. You cannot have duplicate keys in an object. Are
First Name
etc just placeholders?– adiga
Mar 22 at 5:40
1
1
@EthanPrisonMike please use better sample data in your question, something that actually makes sense as a JS object. What is
(Details-Details)
? Is it literally that or something else?– Phil
Mar 22 at 5:42
@EthanPrisonMike please use better sample data in your question, something that actually makes sense as a JS object. What is
(Details-Details)
? Is it literally that or something else?– Phil
Mar 22 at 5:42
|
show 4 more comments
2 Answers
2
active
oldest
votes
split
the string atn
- loop through each line using
map
- Use
match
with the regex/(.*), (.*) (.*)s+(.*)/
to get first name, last name into capturing groups (demo) - create an object from the array using
reduce
let str =
`LastName1, FirstName1 (Details-Details) #ID1
LastName2, FirstName2 (Details-Details) #ID2
LastName3, FirstName3 (Details-Details) #ID3`
let output = str.split("n")
.map(a => a.match(/(.*), (.*) (.*)s+(.*)/))
.reduce((r, [, last, first, id]) =>
r[`$first $last`] = id
return r;
,)
console.log(output)
gonna give this a shot. some last names have commas so I'll have to edit out and reapply after conversion. I'll lyk
– EthanPrisonMike
Mar 22 at 5:55
@EthanPrisonMike it doesn't matter if the last names have,
regex101.com/r/rf7x7B/1
– adiga
Mar 22 at 6:02
lol i meant apostrophes.
– EthanPrisonMike
Mar 22 at 6:05
@EthanPrisonMike that doesn't affect the regex as well. Try it on regex101
– adiga
Mar 22 at 6:06
I'm using vs code. threw the apostrophe at the beginning of the object and end like you did and it's erroring out after the first line. error at the end of the first line is ("unterminated string literal") essentially the 2500 lines won't convert into a giant string. I should probably mention I'm trying to do all this in a js file as well
– EthanPrisonMike
Mar 22 at 6:19
|
show 5 more comments
You can use regex to extract the data, use RegExp#exec
method to extract data using a regular expression.
var str = `Last Name1, First Name1 (Details-Details) j1
Last Name2, First Name2 (Details-Details) 32
Last Name3, First Name3 (Details-Details) 3
Last Nam4e, First Name4 (Details-Details) 4
Last Name5, First Name5 (Details-Details) 5
Last Name6, First Name6 (Details-Details) 6`
// pattern for matching the string
let reg = /([w ]+)bs?,s?([w ]+)bs*([^)]+)s*([wd]+)/g;
// variable for storing match
let m;
// object for the result
let res = ;
// iterate over the matches
while (m = reg.exec(str))
// define object property based on the match
res[`$m[2] $m[1]`] = m[3];
console.log(res);
Regex explanation here.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55293472%2fconvert-a-multi-line-string-into-a-javascript-object%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
split
the string atn
- loop through each line using
map
- Use
match
with the regex/(.*), (.*) (.*)s+(.*)/
to get first name, last name into capturing groups (demo) - create an object from the array using
reduce
let str =
`LastName1, FirstName1 (Details-Details) #ID1
LastName2, FirstName2 (Details-Details) #ID2
LastName3, FirstName3 (Details-Details) #ID3`
let output = str.split("n")
.map(a => a.match(/(.*), (.*) (.*)s+(.*)/))
.reduce((r, [, last, first, id]) =>
r[`$first $last`] = id
return r;
,)
console.log(output)
gonna give this a shot. some last names have commas so I'll have to edit out and reapply after conversion. I'll lyk
– EthanPrisonMike
Mar 22 at 5:55
@EthanPrisonMike it doesn't matter if the last names have,
regex101.com/r/rf7x7B/1
– adiga
Mar 22 at 6:02
lol i meant apostrophes.
– EthanPrisonMike
Mar 22 at 6:05
@EthanPrisonMike that doesn't affect the regex as well. Try it on regex101
– adiga
Mar 22 at 6:06
I'm using vs code. threw the apostrophe at the beginning of the object and end like you did and it's erroring out after the first line. error at the end of the first line is ("unterminated string literal") essentially the 2500 lines won't convert into a giant string. I should probably mention I'm trying to do all this in a js file as well
– EthanPrisonMike
Mar 22 at 6:19
|
show 5 more comments
split
the string atn
- loop through each line using
map
- Use
match
with the regex/(.*), (.*) (.*)s+(.*)/
to get first name, last name into capturing groups (demo) - create an object from the array using
reduce
let str =
`LastName1, FirstName1 (Details-Details) #ID1
LastName2, FirstName2 (Details-Details) #ID2
LastName3, FirstName3 (Details-Details) #ID3`
let output = str.split("n")
.map(a => a.match(/(.*), (.*) (.*)s+(.*)/))
.reduce((r, [, last, first, id]) =>
r[`$first $last`] = id
return r;
,)
console.log(output)
gonna give this a shot. some last names have commas so I'll have to edit out and reapply after conversion. I'll lyk
– EthanPrisonMike
Mar 22 at 5:55
@EthanPrisonMike it doesn't matter if the last names have,
regex101.com/r/rf7x7B/1
– adiga
Mar 22 at 6:02
lol i meant apostrophes.
– EthanPrisonMike
Mar 22 at 6:05
@EthanPrisonMike that doesn't affect the regex as well. Try it on regex101
– adiga
Mar 22 at 6:06
I'm using vs code. threw the apostrophe at the beginning of the object and end like you did and it's erroring out after the first line. error at the end of the first line is ("unterminated string literal") essentially the 2500 lines won't convert into a giant string. I should probably mention I'm trying to do all this in a js file as well
– EthanPrisonMike
Mar 22 at 6:19
|
show 5 more comments
split
the string atn
- loop through each line using
map
- Use
match
with the regex/(.*), (.*) (.*)s+(.*)/
to get first name, last name into capturing groups (demo) - create an object from the array using
reduce
let str =
`LastName1, FirstName1 (Details-Details) #ID1
LastName2, FirstName2 (Details-Details) #ID2
LastName3, FirstName3 (Details-Details) #ID3`
let output = str.split("n")
.map(a => a.match(/(.*), (.*) (.*)s+(.*)/))
.reduce((r, [, last, first, id]) =>
r[`$first $last`] = id
return r;
,)
console.log(output)
split
the string atn
- loop through each line using
map
- Use
match
with the regex/(.*), (.*) (.*)s+(.*)/
to get first name, last name into capturing groups (demo) - create an object from the array using
reduce
let str =
`LastName1, FirstName1 (Details-Details) #ID1
LastName2, FirstName2 (Details-Details) #ID2
LastName3, FirstName3 (Details-Details) #ID3`
let output = str.split("n")
.map(a => a.match(/(.*), (.*) (.*)s+(.*)/))
.reduce((r, [, last, first, id]) =>
r[`$first $last`] = id
return r;
,)
console.log(output)
let str =
`LastName1, FirstName1 (Details-Details) #ID1
LastName2, FirstName2 (Details-Details) #ID2
LastName3, FirstName3 (Details-Details) #ID3`
let output = str.split("n")
.map(a => a.match(/(.*), (.*) (.*)s+(.*)/))
.reduce((r, [, last, first, id]) =>
r[`$first $last`] = id
return r;
,)
console.log(output)
let str =
`LastName1, FirstName1 (Details-Details) #ID1
LastName2, FirstName2 (Details-Details) #ID2
LastName3, FirstName3 (Details-Details) #ID3`
let output = str.split("n")
.map(a => a.match(/(.*), (.*) (.*)s+(.*)/))
.reduce((r, [, last, first, id]) =>
r[`$first $last`] = id
return r;
,)
console.log(output)
edited Mar 22 at 5:55
answered Mar 22 at 5:48
adigaadiga
12.4k62745
12.4k62745
gonna give this a shot. some last names have commas so I'll have to edit out and reapply after conversion. I'll lyk
– EthanPrisonMike
Mar 22 at 5:55
@EthanPrisonMike it doesn't matter if the last names have,
regex101.com/r/rf7x7B/1
– adiga
Mar 22 at 6:02
lol i meant apostrophes.
– EthanPrisonMike
Mar 22 at 6:05
@EthanPrisonMike that doesn't affect the regex as well. Try it on regex101
– adiga
Mar 22 at 6:06
I'm using vs code. threw the apostrophe at the beginning of the object and end like you did and it's erroring out after the first line. error at the end of the first line is ("unterminated string literal") essentially the 2500 lines won't convert into a giant string. I should probably mention I'm trying to do all this in a js file as well
– EthanPrisonMike
Mar 22 at 6:19
|
show 5 more comments
gonna give this a shot. some last names have commas so I'll have to edit out and reapply after conversion. I'll lyk
– EthanPrisonMike
Mar 22 at 5:55
@EthanPrisonMike it doesn't matter if the last names have,
regex101.com/r/rf7x7B/1
– adiga
Mar 22 at 6:02
lol i meant apostrophes.
– EthanPrisonMike
Mar 22 at 6:05
@EthanPrisonMike that doesn't affect the regex as well. Try it on regex101
– adiga
Mar 22 at 6:06
I'm using vs code. threw the apostrophe at the beginning of the object and end like you did and it's erroring out after the first line. error at the end of the first line is ("unterminated string literal") essentially the 2500 lines won't convert into a giant string. I should probably mention I'm trying to do all this in a js file as well
– EthanPrisonMike
Mar 22 at 6:19
gonna give this a shot. some last names have commas so I'll have to edit out and reapply after conversion. I'll lyk
– EthanPrisonMike
Mar 22 at 5:55
gonna give this a shot. some last names have commas so I'll have to edit out and reapply after conversion. I'll lyk
– EthanPrisonMike
Mar 22 at 5:55
@EthanPrisonMike it doesn't matter if the last names have
,
regex101.com/r/rf7x7B/1– adiga
Mar 22 at 6:02
@EthanPrisonMike it doesn't matter if the last names have
,
regex101.com/r/rf7x7B/1– adiga
Mar 22 at 6:02
lol i meant apostrophes.
– EthanPrisonMike
Mar 22 at 6:05
lol i meant apostrophes.
– EthanPrisonMike
Mar 22 at 6:05
@EthanPrisonMike that doesn't affect the regex as well. Try it on regex101
– adiga
Mar 22 at 6:06
@EthanPrisonMike that doesn't affect the regex as well. Try it on regex101
– adiga
Mar 22 at 6:06
I'm using vs code. threw the apostrophe at the beginning of the object and end like you did and it's erroring out after the first line. error at the end of the first line is ("unterminated string literal") essentially the 2500 lines won't convert into a giant string. I should probably mention I'm trying to do all this in a js file as well
– EthanPrisonMike
Mar 22 at 6:19
I'm using vs code. threw the apostrophe at the beginning of the object and end like you did and it's erroring out after the first line. error at the end of the first line is ("unterminated string literal") essentially the 2500 lines won't convert into a giant string. I should probably mention I'm trying to do all this in a js file as well
– EthanPrisonMike
Mar 22 at 6:19
|
show 5 more comments
You can use regex to extract the data, use RegExp#exec
method to extract data using a regular expression.
var str = `Last Name1, First Name1 (Details-Details) j1
Last Name2, First Name2 (Details-Details) 32
Last Name3, First Name3 (Details-Details) 3
Last Nam4e, First Name4 (Details-Details) 4
Last Name5, First Name5 (Details-Details) 5
Last Name6, First Name6 (Details-Details) 6`
// pattern for matching the string
let reg = /([w ]+)bs?,s?([w ]+)bs*([^)]+)s*([wd]+)/g;
// variable for storing match
let m;
// object for the result
let res = ;
// iterate over the matches
while (m = reg.exec(str))
// define object property based on the match
res[`$m[2] $m[1]`] = m[3];
console.log(res);
Regex explanation here.
add a comment |
You can use regex to extract the data, use RegExp#exec
method to extract data using a regular expression.
var str = `Last Name1, First Name1 (Details-Details) j1
Last Name2, First Name2 (Details-Details) 32
Last Name3, First Name3 (Details-Details) 3
Last Nam4e, First Name4 (Details-Details) 4
Last Name5, First Name5 (Details-Details) 5
Last Name6, First Name6 (Details-Details) 6`
// pattern for matching the string
let reg = /([w ]+)bs?,s?([w ]+)bs*([^)]+)s*([wd]+)/g;
// variable for storing match
let m;
// object for the result
let res = ;
// iterate over the matches
while (m = reg.exec(str))
// define object property based on the match
res[`$m[2] $m[1]`] = m[3];
console.log(res);
Regex explanation here.
add a comment |
You can use regex to extract the data, use RegExp#exec
method to extract data using a regular expression.
var str = `Last Name1, First Name1 (Details-Details) j1
Last Name2, First Name2 (Details-Details) 32
Last Name3, First Name3 (Details-Details) 3
Last Nam4e, First Name4 (Details-Details) 4
Last Name5, First Name5 (Details-Details) 5
Last Name6, First Name6 (Details-Details) 6`
// pattern for matching the string
let reg = /([w ]+)bs?,s?([w ]+)bs*([^)]+)s*([wd]+)/g;
// variable for storing match
let m;
// object for the result
let res = ;
// iterate over the matches
while (m = reg.exec(str))
// define object property based on the match
res[`$m[2] $m[1]`] = m[3];
console.log(res);
Regex explanation here.
You can use regex to extract the data, use RegExp#exec
method to extract data using a regular expression.
var str = `Last Name1, First Name1 (Details-Details) j1
Last Name2, First Name2 (Details-Details) 32
Last Name3, First Name3 (Details-Details) 3
Last Nam4e, First Name4 (Details-Details) 4
Last Name5, First Name5 (Details-Details) 5
Last Name6, First Name6 (Details-Details) 6`
// pattern for matching the string
let reg = /([w ]+)bs?,s?([w ]+)bs*([^)]+)s*([wd]+)/g;
// variable for storing match
let m;
// object for the result
let res = ;
// iterate over the matches
while (m = reg.exec(str))
// define object property based on the match
res[`$m[2] $m[1]`] = m[3];
console.log(res);
Regex explanation here.
var str = `Last Name1, First Name1 (Details-Details) j1
Last Name2, First Name2 (Details-Details) 32
Last Name3, First Name3 (Details-Details) 3
Last Nam4e, First Name4 (Details-Details) 4
Last Name5, First Name5 (Details-Details) 5
Last Name6, First Name6 (Details-Details) 6`
// pattern for matching the string
let reg = /([w ]+)bs?,s?([w ]+)bs*([^)]+)s*([wd]+)/g;
// variable for storing match
let m;
// object for the result
let res = ;
// iterate over the matches
while (m = reg.exec(str))
// define object property based on the match
res[`$m[2] $m[1]`] = m[3];
console.log(res);
var str = `Last Name1, First Name1 (Details-Details) j1
Last Name2, First Name2 (Details-Details) 32
Last Name3, First Name3 (Details-Details) 3
Last Nam4e, First Name4 (Details-Details) 4
Last Name5, First Name5 (Details-Details) 5
Last Name6, First Name6 (Details-Details) 6`
// pattern for matching the string
let reg = /([w ]+)bs?,s?([w ]+)bs*([^)]+)s*([wd]+)/g;
// variable for storing match
let m;
// object for the result
let res = ;
// iterate over the matches
while (m = reg.exec(str))
// define object property based on the match
res[`$m[2] $m[1]`] = m[3];
console.log(res);
edited Mar 22 at 6:01
answered Mar 22 at 5:49
Pranav C BalanPranav C Balan
90.2k1391118
90.2k1391118
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55293472%2fconvert-a-multi-line-string-into-a-javascript-object%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Use regex to extract data
– Pranav C Balan
Mar 22 at 5:39
maybe use regex to extract data and convert it to object like you want.
– vibhor1997a
Mar 22 at 5:39
yea it's not JSON. above is it's format when pasted in too notepad
– EthanPrisonMike
Mar 22 at 5:40
4
Please post the actual data. You cannot have duplicate keys in an object. Are
First Name
etc just placeholders?– adiga
Mar 22 at 5:40
1
@EthanPrisonMike please use better sample data in your question, something that actually makes sense as a JS object. What is
(Details-Details)
? Is it literally that or something else?– Phil
Mar 22 at 5:42