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;








2















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?










share|improve this question
























  • FYI, JSON and JavaScript objects are two different things.

    – Felix Kling
    Mar 22 at 18:43

















2















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?










share|improve this question
























  • FYI, JSON and JavaScript objects are two different things.

    – Felix Kling
    Mar 22 at 18:43













2












2








2








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












3 Answers
3






active

oldest

votes


















4














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)








share|improve this answer
































    2














    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)








    share|improve this answer






























      0

















      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)








      share|improve this answer

























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



        );













        draft saved

        draft discarded


















        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









        4














        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)








        share|improve this answer





























          4














          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)








          share|improve this answer



























            4












            4








            4







            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)








            share|improve this answer















            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)






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 22 at 19:12

























            answered Mar 22 at 18:44









            adigaadiga

            13k62745




            13k62745























                2














                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)








                share|improve this answer



























                  2














                  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)








                  share|improve this answer

























                    2












                    2








                    2







                    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)








                    share|improve this answer













                    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)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 22 at 18:44









                    Code ManiacCode Maniac

                    13.1k21034




                    13.1k21034





















                        0

















                        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)








                        share|improve this answer





























                          0

















                          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)








                          share|improve this answer



























                            0












                            0








                            0










                            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)








                            share|improve this answer


















                            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)






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Mar 22 at 19:41

























                            answered Mar 22 at 19:32









                            G.azizG.aziz

                            1,158518




                            1,158518



























                                draft saved

                                draft discarded
















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript