Defining arrays in single line in Javascript
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
add a comment |
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
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 writingvar photos; var tags = new Array();
. Hence the error (photos isundefined
).
– briosheje
Nov 15 '18 at 11:34
add a comment |
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
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
javascript arrays declaration
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 writingvar photos; var tags = new Array();
. Hence the error (photos isundefined
).
– briosheje
Nov 15 '18 at 11:34
add a comment |
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 writingvar photos; var tags = new Array();
. Hence the error (photos isundefined
).
– 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
add a comment |
2 Answers
2
active
oldest
votes
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 , () => );
it works thank you! I will mark as a true answer.
– devneeddev
Nov 15 '18 at 11:30
add a comment |
Using ES6 you can:
let [photos, tags] = [, ];
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%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
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 , () => );
it works thank you! I will mark as a true answer.
– devneeddev
Nov 15 '18 at 11:30
add a comment |
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 , () => );
it works thank you! I will mark as a true answer.
– devneeddev
Nov 15 '18 at 11:30
add a comment |
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 , () => );
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 , () => );
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
add a comment |
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
add a comment |
Using ES6 you can:
let [photos, tags] = [, ];
add a comment |
Using ES6 you can:
let [photos, tags] = [, ];
add a comment |
Using ES6 you can:
let [photos, tags] = [, ];
Using ES6 you can:
let [photos, tags] = [, ];
answered Nov 15 '18 at 11:30
omri_saadonomri_saadon
7,07841546
7,07841546
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%2f53318455%2fdefining-arrays-in-single-line-in-javascript%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
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 writingvar photos; var tags = new Array();
. Hence the error (photos isundefined
).– briosheje
Nov 15 '18 at 11:34