Defining arrays in single line in Javascript










0















I am trying to declare multiple arrays in a single line. But I am taking this error. (Cannot set property "0" of undefined)



var photos,tags = new Array();

flickrJSON.items.forEach(function(item, index)
photos[index] = item.media.m;
tags[index] = item.tags;
);


Why I take this error? Can somebody explain? and How can I fix it










share|improve this question






















  • Can you use ES6?

    – omri_saadon
    Nov 15 '18 at 11:29











  • Just for the sake of interpreting the error, writing: var photos,tags = new Array(); is the same as writing var photos; var tags = new Array();. Hence the error (photos is undefined).

    – briosheje
    Nov 15 '18 at 11:34
















0















I am trying to declare multiple arrays in a single line. But I am taking this error. (Cannot set property "0" of undefined)



var photos,tags = new Array();

flickrJSON.items.forEach(function(item, index)
photos[index] = item.media.m;
tags[index] = item.tags;
);


Why I take this error? Can somebody explain? and How can I fix it










share|improve this question






















  • Can you use ES6?

    – omri_saadon
    Nov 15 '18 at 11:29











  • Just for the sake of interpreting the error, writing: var photos,tags = new Array(); is the same as writing var photos; var tags = new Array();. Hence the error (photos is undefined).

    – briosheje
    Nov 15 '18 at 11:34














0












0








0








I am trying to declare multiple arrays in a single line. But I am taking this error. (Cannot set property "0" of undefined)



var photos,tags = new Array();

flickrJSON.items.forEach(function(item, index)
photos[index] = item.media.m;
tags[index] = item.tags;
);


Why I take this error? Can somebody explain? and How can I fix it










share|improve this question














I am trying to declare multiple arrays in a single line. But I am taking this error. (Cannot set property "0" of undefined)



var photos,tags = new Array();

flickrJSON.items.forEach(function(item, index)
photos[index] = item.media.m;
tags[index] = item.tags;
);


Why I take this error? Can somebody explain? and How can I fix it







javascript arrays declaration






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 11:28









devneeddevdevneeddev

564




564












  • Can you use ES6?

    – omri_saadon
    Nov 15 '18 at 11:29











  • Just for the sake of interpreting the error, writing: var photos,tags = new Array(); is the same as writing var photos; var tags = new Array();. Hence the error (photos is undefined).

    – briosheje
    Nov 15 '18 at 11:34


















  • Can you use ES6?

    – omri_saadon
    Nov 15 '18 at 11:29











  • Just for the sake of interpreting the error, writing: var photos,tags = new Array(); is the same as writing var photos; var tags = new Array();. Hence the error (photos is undefined).

    – briosheje
    Nov 15 '18 at 11:34

















Can you use ES6?

– omri_saadon
Nov 15 '18 at 11:29





Can you use ES6?

– omri_saadon
Nov 15 '18 at 11:29













Just for the sake of interpreting the error, writing: var photos,tags = new Array(); is the same as writing var photos; var tags = new Array();. Hence the error (photos is undefined).

– briosheje
Nov 15 '18 at 11:34






Just for the sake of interpreting the error, writing: var photos,tags = new Array(); is the same as writing var photos; var tags = new Array();. Hence the error (photos is undefined).

– briosheje
Nov 15 '18 at 11:34













2 Answers
2






active

oldest

votes


















3














You're trying to use two arrays there - an array named photos, and an array named tags, so you need to use new Array (or, preferably, ) twice:



var photos = , tags = ;


In your original code, var photos, will result in photos being undefined, no matter what comes after the comma.



If you wanted to create a lot of arrays at once and didn't want to repeat = each time, you could use Array.from with destructuring to keep code DRY:



const [arr1, arr2, arr3, arr4, arr5] = Array.from( length: 5 , () => );





share|improve this answer

























  • it works thank you! I will mark as a true answer.

    – devneeddev
    Nov 15 '18 at 11:30



















2














Using ES6 you can:



let [photos, tags] = [, ];





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%2f53318455%2fdefining-arrays-in-single-line-in-javascript%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









    3














    You're trying to use two arrays there - an array named photos, and an array named tags, so you need to use new Array (or, preferably, ) twice:



    var photos = , tags = ;


    In your original code, var photos, will result in photos being undefined, no matter what comes after the comma.



    If you wanted to create a lot of arrays at once and didn't want to repeat = each time, you could use Array.from with destructuring to keep code DRY:



    const [arr1, arr2, arr3, arr4, arr5] = Array.from( length: 5 , () => );





    share|improve this answer

























    • it works thank you! I will mark as a true answer.

      – devneeddev
      Nov 15 '18 at 11:30
















    3














    You're trying to use two arrays there - an array named photos, and an array named tags, so you need to use new Array (or, preferably, ) twice:



    var photos = , tags = ;


    In your original code, var photos, will result in photos being undefined, no matter what comes after the comma.



    If you wanted to create a lot of arrays at once and didn't want to repeat = each time, you could use Array.from with destructuring to keep code DRY:



    const [arr1, arr2, arr3, arr4, arr5] = Array.from( length: 5 , () => );





    share|improve this answer

























    • it works thank you! I will mark as a true answer.

      – devneeddev
      Nov 15 '18 at 11:30














    3












    3








    3







    You're trying to use two arrays there - an array named photos, and an array named tags, so you need to use new Array (or, preferably, ) twice:



    var photos = , tags = ;


    In your original code, var photos, will result in photos being undefined, no matter what comes after the comma.



    If you wanted to create a lot of arrays at once and didn't want to repeat = each time, you could use Array.from with destructuring to keep code DRY:



    const [arr1, arr2, arr3, arr4, arr5] = Array.from( length: 5 , () => );





    share|improve this answer















    You're trying to use two arrays there - an array named photos, and an array named tags, so you need to use new Array (or, preferably, ) twice:



    var photos = , tags = ;


    In your original code, var photos, will result in photos being undefined, no matter what comes after the comma.



    If you wanted to create a lot of arrays at once and didn't want to repeat = each time, you could use Array.from with destructuring to keep code DRY:



    const [arr1, arr2, arr3, arr4, arr5] = Array.from( length: 5 , () => );






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 15 '18 at 11:32

























    answered Nov 15 '18 at 11:29









    CertainPerformanceCertainPerformance

    93.4k165484




    93.4k165484












    • it works thank you! I will mark as a true answer.

      – devneeddev
      Nov 15 '18 at 11:30


















    • it works thank you! I will mark as a true answer.

      – devneeddev
      Nov 15 '18 at 11:30

















    it works thank you! I will mark as a true answer.

    – devneeddev
    Nov 15 '18 at 11:30






    it works thank you! I will mark as a true answer.

    – devneeddev
    Nov 15 '18 at 11:30














    2














    Using ES6 you can:



    let [photos, tags] = [, ];





    share|improve this answer



























      2














      Using ES6 you can:



      let [photos, tags] = [, ];





      share|improve this answer

























        2












        2








        2







        Using ES6 you can:



        let [photos, tags] = [, ];





        share|improve this answer













        Using ES6 you can:



        let [photos, tags] = [, ];






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 11:30









        omri_saadonomri_saadon

        7,07841546




        7,07841546



























            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%2f53318455%2fdefining-arrays-in-single-line-in-javascript%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







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3