How can I get first value only in each JSON object












1















x variable has a JSON value like below



let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]  


the output should be



y = [7,1];


How can I get the JSON multiple object first value only? Thanks.










share|improve this question

























  • This question has nothing to do with JSON.

    – Brad
    Nov 13 '18 at 3:01
















1















x variable has a JSON value like below



let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]  


the output should be



y = [7,1];


How can I get the JSON multiple object first value only? Thanks.










share|improve this question

























  • This question has nothing to do with JSON.

    – Brad
    Nov 13 '18 at 3:01














1












1








1








x variable has a JSON value like below



let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]  


the output should be



y = [7,1];


How can I get the JSON multiple object first value only? Thanks.










share|improve this question
















x variable has a JSON value like below



let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]  


the output should be



y = [7,1];


How can I get the JSON multiple object first value only? Thanks.







javascript arrays json object






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 3:22









hev1

5,5683527




5,5683527










asked Nov 13 '18 at 2:52









imjayabalimjayabal

308




308













  • This question has nothing to do with JSON.

    – Brad
    Nov 13 '18 at 3:01



















  • This question has nothing to do with JSON.

    – Brad
    Nov 13 '18 at 3:01

















This question has nothing to do with JSON.

– Brad
Nov 13 '18 at 3:01





This question has nothing to do with JSON.

– Brad
Nov 13 '18 at 3:01












4 Answers
4






active

oldest

votes


















3














Object properties are only reliably ordered in semi-modern browsers - your desired output may not be reliably determinable from code on running older browsers (unless you have a set order of the property names in an array, or unless the properties are numeric, or something like that). But, on newer browsers, you can .map the input array and extract the first value found by Object.values from the object you're iterating over:






const x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
const result = x.map((obj) => Object.values(obj)[0]);
console.log(result);





Also note that, as always, there's no such thing as a "JSON Object". If you have an object or array, then you have an object or array, full stop. JSON format is a method of representing an object in a string, like const myJSON = '{"foo":"bar"}'. If there are no strings, serialization, or deserialization involved, then JSON is not involved either.






share|improve this answer
























  • If Object.values not available, may be: const result = x.map((obj) => obj[Object.keys(obj)[0]]);

    – vladimir.shein
    Nov 13 '18 at 3:11













  • Opps ..missed that

    – charlietfl
    Nov 13 '18 at 3:13











  • What about this const result = x.map((obj) => obj.a); can I use like this?

    – imjayabal
    Nov 13 '18 at 5:01













  • @imjayabal Sure, if you know the name of the property you want to extract, better to use that property - no need for Object.values in that case.

    – CertainPerformance
    Nov 13 '18 at 5:02



















1














If you're sure about the key you want to access, then you just need to iterate from x and then push the result on your y.



let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
let y = ;

for(let i in x) {
y.push(x[i].a);
}

console.log(y);





share|improve this answer































    0














    You could combine Array#map with Object.values() to achieve this; the mapping would transform each item of your input array x to the resulting array y. For each item in the resulting array y, you would select the first value of the corresponding item from x.



    To select the first value of an item in x, that item is passed to Object.values(). The result of Object.values() is an array of the values of that item, and it's by this that you can access and "map" the first value of each item in y:






    let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]

    let y = x.map(item => Object.values(item)[0]);

    console.log(y)








    share|improve this answer































      0














      var data = ;
      x.forEach((elem) => {
      data.push(elem.a);
      });





      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%2f53273090%2fhow-can-i-get-first-value-only-in-each-json-object%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3














        Object properties are only reliably ordered in semi-modern browsers - your desired output may not be reliably determinable from code on running older browsers (unless you have a set order of the property names in an array, or unless the properties are numeric, or something like that). But, on newer browsers, you can .map the input array and extract the first value found by Object.values from the object you're iterating over:






        const x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
        const result = x.map((obj) => Object.values(obj)[0]);
        console.log(result);





        Also note that, as always, there's no such thing as a "JSON Object". If you have an object or array, then you have an object or array, full stop. JSON format is a method of representing an object in a string, like const myJSON = '{"foo":"bar"}'. If there are no strings, serialization, or deserialization involved, then JSON is not involved either.






        share|improve this answer
























        • If Object.values not available, may be: const result = x.map((obj) => obj[Object.keys(obj)[0]]);

          – vladimir.shein
          Nov 13 '18 at 3:11













        • Opps ..missed that

          – charlietfl
          Nov 13 '18 at 3:13











        • What about this const result = x.map((obj) => obj.a); can I use like this?

          – imjayabal
          Nov 13 '18 at 5:01













        • @imjayabal Sure, if you know the name of the property you want to extract, better to use that property - no need for Object.values in that case.

          – CertainPerformance
          Nov 13 '18 at 5:02
















        3














        Object properties are only reliably ordered in semi-modern browsers - your desired output may not be reliably determinable from code on running older browsers (unless you have a set order of the property names in an array, or unless the properties are numeric, or something like that). But, on newer browsers, you can .map the input array and extract the first value found by Object.values from the object you're iterating over:






        const x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
        const result = x.map((obj) => Object.values(obj)[0]);
        console.log(result);





        Also note that, as always, there's no such thing as a "JSON Object". If you have an object or array, then you have an object or array, full stop. JSON format is a method of representing an object in a string, like const myJSON = '{"foo":"bar"}'. If there are no strings, serialization, or deserialization involved, then JSON is not involved either.






        share|improve this answer
























        • If Object.values not available, may be: const result = x.map((obj) => obj[Object.keys(obj)[0]]);

          – vladimir.shein
          Nov 13 '18 at 3:11













        • Opps ..missed that

          – charlietfl
          Nov 13 '18 at 3:13











        • What about this const result = x.map((obj) => obj.a); can I use like this?

          – imjayabal
          Nov 13 '18 at 5:01













        • @imjayabal Sure, if you know the name of the property you want to extract, better to use that property - no need for Object.values in that case.

          – CertainPerformance
          Nov 13 '18 at 5:02














        3












        3








        3







        Object properties are only reliably ordered in semi-modern browsers - your desired output may not be reliably determinable from code on running older browsers (unless you have a set order of the property names in an array, or unless the properties are numeric, or something like that). But, on newer browsers, you can .map the input array and extract the first value found by Object.values from the object you're iterating over:






        const x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
        const result = x.map((obj) => Object.values(obj)[0]);
        console.log(result);





        Also note that, as always, there's no such thing as a "JSON Object". If you have an object or array, then you have an object or array, full stop. JSON format is a method of representing an object in a string, like const myJSON = '{"foo":"bar"}'. If there are no strings, serialization, or deserialization involved, then JSON is not involved either.






        share|improve this answer













        Object properties are only reliably ordered in semi-modern browsers - your desired output may not be reliably determinable from code on running older browsers (unless you have a set order of the property names in an array, or unless the properties are numeric, or something like that). But, on newer browsers, you can .map the input array and extract the first value found by Object.values from the object you're iterating over:






        const x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
        const result = x.map((obj) => Object.values(obj)[0]);
        console.log(result);





        Also note that, as always, there's no such thing as a "JSON Object". If you have an object or array, then you have an object or array, full stop. JSON format is a method of representing an object in a string, like const myJSON = '{"foo":"bar"}'. If there are no strings, serialization, or deserialization involved, then JSON is not involved either.






        const x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
        const result = x.map((obj) => Object.values(obj)[0]);
        console.log(result);





        const x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
        const result = x.map((obj) => Object.values(obj)[0]);
        console.log(result);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 2:56









        CertainPerformanceCertainPerformance

        79.5k143865




        79.5k143865













        • If Object.values not available, may be: const result = x.map((obj) => obj[Object.keys(obj)[0]]);

          – vladimir.shein
          Nov 13 '18 at 3:11













        • Opps ..missed that

          – charlietfl
          Nov 13 '18 at 3:13











        • What about this const result = x.map((obj) => obj.a); can I use like this?

          – imjayabal
          Nov 13 '18 at 5:01













        • @imjayabal Sure, if you know the name of the property you want to extract, better to use that property - no need for Object.values in that case.

          – CertainPerformance
          Nov 13 '18 at 5:02



















        • If Object.values not available, may be: const result = x.map((obj) => obj[Object.keys(obj)[0]]);

          – vladimir.shein
          Nov 13 '18 at 3:11













        • Opps ..missed that

          – charlietfl
          Nov 13 '18 at 3:13











        • What about this const result = x.map((obj) => obj.a); can I use like this?

          – imjayabal
          Nov 13 '18 at 5:01













        • @imjayabal Sure, if you know the name of the property you want to extract, better to use that property - no need for Object.values in that case.

          – CertainPerformance
          Nov 13 '18 at 5:02

















        If Object.values not available, may be: const result = x.map((obj) => obj[Object.keys(obj)[0]]);

        – vladimir.shein
        Nov 13 '18 at 3:11







        If Object.values not available, may be: const result = x.map((obj) => obj[Object.keys(obj)[0]]);

        – vladimir.shein
        Nov 13 '18 at 3:11















        Opps ..missed that

        – charlietfl
        Nov 13 '18 at 3:13





        Opps ..missed that

        – charlietfl
        Nov 13 '18 at 3:13













        What about this const result = x.map((obj) => obj.a); can I use like this?

        – imjayabal
        Nov 13 '18 at 5:01







        What about this const result = x.map((obj) => obj.a); can I use like this?

        – imjayabal
        Nov 13 '18 at 5:01















        @imjayabal Sure, if you know the name of the property you want to extract, better to use that property - no need for Object.values in that case.

        – CertainPerformance
        Nov 13 '18 at 5:02





        @imjayabal Sure, if you know the name of the property you want to extract, better to use that property - no need for Object.values in that case.

        – CertainPerformance
        Nov 13 '18 at 5:02













        1














        If you're sure about the key you want to access, then you just need to iterate from x and then push the result on your y.



        let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
        let y = ;

        for(let i in x) {
        y.push(x[i].a);
        }

        console.log(y);





        share|improve this answer




























          1














          If you're sure about the key you want to access, then you just need to iterate from x and then push the result on your y.



          let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
          let y = ;

          for(let i in x) {
          y.push(x[i].a);
          }

          console.log(y);





          share|improve this answer


























            1












            1








            1







            If you're sure about the key you want to access, then you just need to iterate from x and then push the result on your y.



            let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
            let y = ;

            for(let i in x) {
            y.push(x[i].a);
            }

            console.log(y);





            share|improve this answer













            If you're sure about the key you want to access, then you just need to iterate from x and then push the result on your y.



            let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }];
            let y = ;

            for(let i in x) {
            y.push(x[i].a);
            }

            console.log(y);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 '18 at 3:19









            Match SumayaMatch Sumaya

            112




            112























                0














                You could combine Array#map with Object.values() to achieve this; the mapping would transform each item of your input array x to the resulting array y. For each item in the resulting array y, you would select the first value of the corresponding item from x.



                To select the first value of an item in x, that item is passed to Object.values(). The result of Object.values() is an array of the values of that item, and it's by this that you can access and "map" the first value of each item in y:






                let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]

                let y = x.map(item => Object.values(item)[0]);

                console.log(y)








                share|improve this answer




























                  0














                  You could combine Array#map with Object.values() to achieve this; the mapping would transform each item of your input array x to the resulting array y. For each item in the resulting array y, you would select the first value of the corresponding item from x.



                  To select the first value of an item in x, that item is passed to Object.values(). The result of Object.values() is an array of the values of that item, and it's by this that you can access and "map" the first value of each item in y:






                  let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]

                  let y = x.map(item => Object.values(item)[0]);

                  console.log(y)








                  share|improve this answer


























                    0












                    0








                    0







                    You could combine Array#map with Object.values() to achieve this; the mapping would transform each item of your input array x to the resulting array y. For each item in the resulting array y, you would select the first value of the corresponding item from x.



                    To select the first value of an item in x, that item is passed to Object.values(). The result of Object.values() is an array of the values of that item, and it's by this that you can access and "map" the first value of each item in y:






                    let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]

                    let y = x.map(item => Object.values(item)[0]);

                    console.log(y)








                    share|improve this answer













                    You could combine Array#map with Object.values() to achieve this; the mapping would transform each item of your input array x to the resulting array y. For each item in the resulting array y, you would select the first value of the corresponding item from x.



                    To select the first value of an item in x, that item is passed to Object.values(). The result of Object.values() is an array of the values of that item, and it's by this that you can access and "map" the first value of each item in y:






                    let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]

                    let y = x.map(item => Object.values(item)[0]);

                    console.log(y)








                    let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]

                    let y = x.map(item => Object.values(item)[0]);

                    console.log(y)





                    let x = [{'a': 7, 'b': 8}, {'a': 1, 'b': 5 }]

                    let y = x.map(item => Object.values(item)[0]);

                    console.log(y)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 13 '18 at 2:56









                    Dacre DennyDacre Denny

                    10.9k4929




                    10.9k4929























                        0














                        var data = ;
                        x.forEach((elem) => {
                        data.push(elem.a);
                        });





                        share|improve this answer






























                          0














                          var data = ;
                          x.forEach((elem) => {
                          data.push(elem.a);
                          });





                          share|improve this answer




























                            0












                            0








                            0







                            var data = ;
                            x.forEach((elem) => {
                            data.push(elem.a);
                            });





                            share|improve this answer















                            var data = ;
                            x.forEach((elem) => {
                            data.push(elem.a);
                            });






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 13 '18 at 8:20









                            dferenc

                            4,531122131




                            4,531122131










                            answered Nov 13 '18 at 5:53









                            Ashish KirodianAshish Kirodian

                            765




                            765






























                                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%2f53273090%2fhow-can-i-get-first-value-only-in-each-json-object%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

                                Florida Star v. B. J. F.

                                Danny Elfman

                                Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues