How to split 1 key value into 2 keys valuesHow do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to append something to an array?How do I redirect to another webpage?Checking if a key exists in a JavaScript object?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?How to get all properties values of a Javascript Object (without knowing the keys)?
Don’t seats that recline flat defeat the purpose of having seatbelts?
Why does nature favour the Laplacian?
How come there are so many candidates for the 2020 Democratic party presidential nomination?
US visa is under administrative processing, I need the passport back ASAP
Packing rectangles: Does rotation ever help?
Stop and Take a Breath!
What makes accurate emulation of old systems a difficult task?
How to stop co-workers from teasing me because I know Russian?
Who is the Umpire in this picture?
A Strange Latex Symbol
Will a top journal at least read my introduction?
What is the incentive for curl to release the library for free?
Why do games have consumables?
How to solve constants out of the internal energy equation?
How much cash can I safely carry into the USA and avoid civil forfeiture?
How to verbalise code in Mathematica?
Do I have to worry about players making “bad” choices on level up?
What is the strongest case that can be made in favour of the UK regaining some control over fishing policy after Brexit?
What is the difference between `command a[bc]d` and `command `ab,cd`
Does this extra sentence in the description of the warlock's Eyes of the Rune Keeper eldritch invocation appear in any official reference?
Combinable filters
Is there really no use for MD5 anymore?
Error message with tabularx
What happened to Captain America in Endgame?
How to split 1 key value into 2 keys values
How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to append something to an array?How do I redirect to another webpage?Checking if a key exists in a JavaScript object?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?How to get all properties values of a Javascript Object (without knowing the keys)?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm new to JavaScript. I have the following object:
let obj1 =
[
'id': 1,
'longString': 'Joe - 2011-04-23T18:25:23.511Z'
,
'id': 2,
'longString': 'Mary - 2010-04-23T18:25:23.511Z'
];
I want to split the longString
key into 2 keys, and the value into 2 values.
Here is what I want to make:
let obj2 =
[
'id': 1,
'name': 'Joe',
'date': '2011-04-23T18:25:23.511Z'
,
'id': 2,
'name': 'Mary',
'date': '2010-04-23T18:25:23.511Z'
];
I've tried to do it in pieces, but I'm stuck with the first piece which is splitting the value of longString
.
This works for string:
let v = 'Mary - 2010-04-23T18:25:23.511Z';
let w = v.split(' -', 1);
How do I make it work for an object? Or is there a totally different way to split property values? Also, do I need to write a loop to assign obj1
to obj2
?
javascript arrays
add a comment |
I'm new to JavaScript. I have the following object:
let obj1 =
[
'id': 1,
'longString': 'Joe - 2011-04-23T18:25:23.511Z'
,
'id': 2,
'longString': 'Mary - 2010-04-23T18:25:23.511Z'
];
I want to split the longString
key into 2 keys, and the value into 2 values.
Here is what I want to make:
let obj2 =
[
'id': 1,
'name': 'Joe',
'date': '2011-04-23T18:25:23.511Z'
,
'id': 2,
'name': 'Mary',
'date': '2010-04-23T18:25:23.511Z'
];
I've tried to do it in pieces, but I'm stuck with the first piece which is splitting the value of longString
.
This works for string:
let v = 'Mary - 2010-04-23T18:25:23.511Z';
let w = v.split(' -', 1);
How do I make it work for an object? Or is there a totally different way to split property values? Also, do I need to write a loop to assign obj1
to obj2
?
javascript arrays
FYI, JSON and JavaScript objects are two different things.
– Felix Kling
Mar 22 at 18:43
add a comment |
I'm new to JavaScript. I have the following object:
let obj1 =
[
'id': 1,
'longString': 'Joe - 2011-04-23T18:25:23.511Z'
,
'id': 2,
'longString': 'Mary - 2010-04-23T18:25:23.511Z'
];
I want to split the longString
key into 2 keys, and the value into 2 values.
Here is what I want to make:
let obj2 =
[
'id': 1,
'name': 'Joe',
'date': '2011-04-23T18:25:23.511Z'
,
'id': 2,
'name': 'Mary',
'date': '2010-04-23T18:25:23.511Z'
];
I've tried to do it in pieces, but I'm stuck with the first piece which is splitting the value of longString
.
This works for string:
let v = 'Mary - 2010-04-23T18:25:23.511Z';
let w = v.split(' -', 1);
How do I make it work for an object? Or is there a totally different way to split property values? Also, do I need to write a loop to assign obj1
to obj2
?
javascript arrays
I'm new to JavaScript. I have the following object:
let obj1 =
[
'id': 1,
'longString': 'Joe - 2011-04-23T18:25:23.511Z'
,
'id': 2,
'longString': 'Mary - 2010-04-23T18:25:23.511Z'
];
I want to split the longString
key into 2 keys, and the value into 2 values.
Here is what I want to make:
let obj2 =
[
'id': 1,
'name': 'Joe',
'date': '2011-04-23T18:25:23.511Z'
,
'id': 2,
'name': 'Mary',
'date': '2010-04-23T18:25:23.511Z'
];
I've tried to do it in pieces, but I'm stuck with the first piece which is splitting the value of longString
.
This works for string:
let v = 'Mary - 2010-04-23T18:25:23.511Z';
let w = v.split(' -', 1);
How do I make it work for an object? Or is there a totally different way to split property values? Also, do I need to write a loop to assign obj1
to obj2
?
javascript arrays
javascript arrays
edited Mar 22 at 19:12
adiga
13k62745
13k62745
asked Mar 22 at 18:41
vt2424253vt2424253
47831127
47831127
FYI, JSON and JavaScript objects are two different things.
– Felix Kling
Mar 22 at 18:43
add a comment |
FYI, JSON and JavaScript objects are two different things.
– Felix Kling
Mar 22 at 18:43
FYI, JSON and JavaScript objects are two different things.
– Felix Kling
Mar 22 at 18:43
FYI, JSON and JavaScript objects are two different things.
– Felix Kling
Mar 22 at 18:43
add a comment |
3 Answers
3
active
oldest
votes
You could map
over the array and split the longString
for each object. Use destructuring to get the splits into name
and date
variables
let obj1 =
[
'id': 1,
'longString': 'Joe - 2011-04-23T18:25:23.511Z'
,
'id': 2,
'longString': 'Mary - 2010-04-23T18:25:23.511Z'
];
const output = obj1.map(a =>
let [name, date] = a.longString.split(" - ");
return id: a.id, name, date
)
console.log(output)
add a comment |
You can simply use map
and s+-s+
pattern to split. second parameter of split is to specify the number of element we want in output
let obj1 =['id': 1,'longString': 'Joe - 2011-04-23T18:25:23.511Z', 'id': 2, 'longString': 'Mary - 2010-04-23T18:25:23.511Z'];
let op = obj1.map((id, longString) =>
let [name, date] = longString.split(/s+-s+/,2)
return id, name, date
)
console.log(op)
add a comment |
let obj1 = [
'id': 1,
'longString': 'Joe - 2011-04-23T18:25:23.511Z'
,
'id': 2,
'longString': 'Mary - 2010-04-23T18:25:23.511Z'
];
const res = obj1.map((
id,
longString
) => (
id,
name: longString.match(/w+/)[0],
date: longString.match(/d+.*/)[0],
))
console.log(res)
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%2f55305960%2fhow-to-split-1-key-value-into-2-keys-values%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could map
over the array and split the longString
for each object. Use destructuring to get the splits into name
and date
variables
let obj1 =
[
'id': 1,
'longString': 'Joe - 2011-04-23T18:25:23.511Z'
,
'id': 2,
'longString': 'Mary - 2010-04-23T18:25:23.511Z'
];
const output = obj1.map(a =>
let [name, date] = a.longString.split(" - ");
return id: a.id, name, date
)
console.log(output)
add a comment |
You could map
over the array and split the longString
for each object. Use destructuring to get the splits into name
and date
variables
let obj1 =
[
'id': 1,
'longString': 'Joe - 2011-04-23T18:25:23.511Z'
,
'id': 2,
'longString': 'Mary - 2010-04-23T18:25:23.511Z'
];
const output = obj1.map(a =>
let [name, date] = a.longString.split(" - ");
return id: a.id, name, date
)
console.log(output)
add a comment |
You could map
over the array and split the longString
for each object. Use destructuring to get the splits into name
and date
variables
let obj1 =
[
'id': 1,
'longString': 'Joe - 2011-04-23T18:25:23.511Z'
,
'id': 2,
'longString': 'Mary - 2010-04-23T18:25:23.511Z'
];
const output = obj1.map(a =>
let [name, date] = a.longString.split(" - ");
return id: a.id, name, date
)
console.log(output)
You could map
over the array and split the longString
for each object. Use destructuring to get the splits into name
and date
variables
let obj1 =
[
'id': 1,
'longString': 'Joe - 2011-04-23T18:25:23.511Z'
,
'id': 2,
'longString': 'Mary - 2010-04-23T18:25:23.511Z'
];
const output = obj1.map(a =>
let [name, date] = a.longString.split(" - ");
return id: a.id, name, date
)
console.log(output)
let obj1 =
[
'id': 1,
'longString': 'Joe - 2011-04-23T18:25:23.511Z'
,
'id': 2,
'longString': 'Mary - 2010-04-23T18:25:23.511Z'
];
const output = obj1.map(a =>
let [name, date] = a.longString.split(" - ");
return id: a.id, name, date
)
console.log(output)
let obj1 =
[
'id': 1,
'longString': 'Joe - 2011-04-23T18:25:23.511Z'
,
'id': 2,
'longString': 'Mary - 2010-04-23T18:25:23.511Z'
];
const output = obj1.map(a =>
let [name, date] = a.longString.split(" - ");
return id: a.id, name, date
)
console.log(output)
edited Mar 22 at 19:12
answered Mar 22 at 18:44
adigaadiga
13k62745
13k62745
add a comment |
add a comment |
You can simply use map
and s+-s+
pattern to split. second parameter of split is to specify the number of element we want in output
let obj1 =['id': 1,'longString': 'Joe - 2011-04-23T18:25:23.511Z', 'id': 2, 'longString': 'Mary - 2010-04-23T18:25:23.511Z'];
let op = obj1.map((id, longString) =>
let [name, date] = longString.split(/s+-s+/,2)
return id, name, date
)
console.log(op)
add a comment |
You can simply use map
and s+-s+
pattern to split. second parameter of split is to specify the number of element we want in output
let obj1 =['id': 1,'longString': 'Joe - 2011-04-23T18:25:23.511Z', 'id': 2, 'longString': 'Mary - 2010-04-23T18:25:23.511Z'];
let op = obj1.map((id, longString) =>
let [name, date] = longString.split(/s+-s+/,2)
return id, name, date
)
console.log(op)
add a comment |
You can simply use map
and s+-s+
pattern to split. second parameter of split is to specify the number of element we want in output
let obj1 =['id': 1,'longString': 'Joe - 2011-04-23T18:25:23.511Z', 'id': 2, 'longString': 'Mary - 2010-04-23T18:25:23.511Z'];
let op = obj1.map((id, longString) =>
let [name, date] = longString.split(/s+-s+/,2)
return id, name, date
)
console.log(op)
You can simply use map
and s+-s+
pattern to split. second parameter of split is to specify the number of element we want in output
let obj1 =['id': 1,'longString': 'Joe - 2011-04-23T18:25:23.511Z', 'id': 2, 'longString': 'Mary - 2010-04-23T18:25:23.511Z'];
let op = obj1.map((id, longString) =>
let [name, date] = longString.split(/s+-s+/,2)
return id, name, date
)
console.log(op)
let obj1 =['id': 1,'longString': 'Joe - 2011-04-23T18:25:23.511Z', 'id': 2, 'longString': 'Mary - 2010-04-23T18:25:23.511Z'];
let op = obj1.map((id, longString) =>
let [name, date] = longString.split(/s+-s+/,2)
return id, name, date
)
console.log(op)
let obj1 =['id': 1,'longString': 'Joe - 2011-04-23T18:25:23.511Z', 'id': 2, 'longString': 'Mary - 2010-04-23T18:25:23.511Z'];
let op = obj1.map((id, longString) =>
let [name, date] = longString.split(/s+-s+/,2)
return id, name, date
)
console.log(op)
answered Mar 22 at 18:44
Code ManiacCode Maniac
13.1k21034
13.1k21034
add a comment |
add a comment |
let obj1 = [
'id': 1,
'longString': 'Joe - 2011-04-23T18:25:23.511Z'
,
'id': 2,
'longString': 'Mary - 2010-04-23T18:25:23.511Z'
];
const res = obj1.map((
id,
longString
) => (
id,
name: longString.match(/w+/)[0],
date: longString.match(/d+.*/)[0],
))
console.log(res)
add a comment |
let obj1 = [
'id': 1,
'longString': 'Joe - 2011-04-23T18:25:23.511Z'
,
'id': 2,
'longString': 'Mary - 2010-04-23T18:25:23.511Z'
];
const res = obj1.map((
id,
longString
) => (
id,
name: longString.match(/w+/)[0],
date: longString.match(/d+.*/)[0],
))
console.log(res)
add a comment |
let obj1 = [
'id': 1,
'longString': 'Joe - 2011-04-23T18:25:23.511Z'
,
'id': 2,
'longString': 'Mary - 2010-04-23T18:25:23.511Z'
];
const res = obj1.map((
id,
longString
) => (
id,
name: longString.match(/w+/)[0],
date: longString.match(/d+.*/)[0],
))
console.log(res)
let obj1 = [
'id': 1,
'longString': 'Joe - 2011-04-23T18:25:23.511Z'
,
'id': 2,
'longString': 'Mary - 2010-04-23T18:25:23.511Z'
];
const res = obj1.map((
id,
longString
) => (
id,
name: longString.match(/w+/)[0],
date: longString.match(/d+.*/)[0],
))
console.log(res)
let obj1 = [
'id': 1,
'longString': 'Joe - 2011-04-23T18:25:23.511Z'
,
'id': 2,
'longString': 'Mary - 2010-04-23T18:25:23.511Z'
];
const res = obj1.map((
id,
longString
) => (
id,
name: longString.match(/w+/)[0],
date: longString.match(/d+.*/)[0],
))
console.log(res)
let obj1 = [
'id': 1,
'longString': 'Joe - 2011-04-23T18:25:23.511Z'
,
'id': 2,
'longString': 'Mary - 2010-04-23T18:25:23.511Z'
];
const res = obj1.map((
id,
longString
) => (
id,
name: longString.match(/w+/)[0],
date: longString.match(/d+.*/)[0],
))
console.log(res)
edited Mar 22 at 19:41
answered Mar 22 at 19:32
G.azizG.aziz
1,158518
1,158518
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%2f55305960%2fhow-to-split-1-key-value-into-2-keys-values%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
FYI, JSON and JavaScript objects are two different things.
– Felix Kling
Mar 22 at 18:43