Why is it that my array is defined but the elements inside it are not? #2280
-
Hi everyone! I created this code in jsPsych:
The problem is that the 'holdingCell' array elements seem to be undefined even though the array itself is defined. That is, the function 'console.log(holdingCell)' returns an array of 10 objects, as it should. However, the function 'console.log(holdingCell[0])' returns 'undefined.' It is as though the array only exists as a whole, where it is impossible to access any individual element in the array. Has anyone encountered this problem before? If so, would you please detail how you solved it? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It looks like you're adding elements to the So try moving your on_timeline_finish: function() {
var imageNames = ['salad', 'pear', 'lollipop', 'grapes', 'cotton candy', 'cookie', 'carrot', 'cake', 'brownie', 'apple']; //list the category names
for (let i = 0; i < imageNames.length; i++) {
// ...
// add elements to holdingCell
holdingCell.push({
// ...
});
}
// check contents of holdingCell
console.log(holdingCell);
console.log(holdingCell[0]);
} Does that give you the values you expected? |
Beta Was this translation helpful? Give feedback.
It looks like you're adding elements to the
holdingCell
array inside theon_timeline_finish
function, which means this array won't have any elements until thelikingRatingTask_1
timeline ends. However yourconsole.log
lines are in your main script, which runs when the page first loads, at which point theholdingCell
array doesn't contain anything.So try moving your
console.log
lines into the end of theon_timeline_finish
function, so that you can check the value of the array after the elements have been added: