1

I keepy getting TypeError: testsession.testset[0].athletes is undefined - i have tried lots of different ways, is it not possible to have an array of arrays of objects

var testsession = {};
var testsetname = {};
var testset = [];
testsession.testsetname = testsetname;
testsession.testsetname = "week9";
testsession.testset = testset;
testsession.testset.push("400M");
testsession.testset.push("800M");

var athletes = [];
var Time = "49.2";
var AthleteID = "a92";
var athlete = { "AthleteID": AthleteID, "Time": Time};
//console.log(pointer);
testsession.testset[0].athletes = athletes;
testsession.testset[0].athletes.push(athlete)
console.log(testsession.testset[0].athletes[0]);

4 Answers 4

1

The testset[0] is a string. Make it an object

var testsession = {};
var testsetname = {};
var testset = [];
testsession.testsetname = testsetname;
testsession.testsetname = "week9";
testsession.testset = testset;

//Earlier you pushed 400m directly which is a string hence causing the error later on
testsession.testset.push({distance: "400M"});
testsession.testset.push({distance: "800M"});

var athletes = [];
var Time = "49.2";
var AthleteID = "a92";
var athlete = { "AthleteID": AthleteID, "Time": Time};
//console.log(pointer);
testsession.testset[0].athletes = athletes;
testsession.testset[0].athletes.push(athlete)
console.log(testsession.testset[0].athletes[0]);
Sign up to request clarification or add additional context in comments.

Comments

1

testsession.testset[0] is a primitive value, a string.

The following statement will therefore not have the effect you may think it has:

testsession.testset[0].athletes = athletes;

What happens here? The primitive at the left has no athletes property, but JavaScript will coerce it to a String object, then assign that property to that temporary String object, which then disappears into oblivion.

So it is like that assignment never happened: testsession.testset[0] will remain a primitive value, and primitive values have no properties.

When you read the athletes property, the same will happen again: JavaScript coerces it to a String object, only to find that object has no athletes property, and so you get undefined.

Comments

1

When you try to access testsession.testset[0] that entry is a string. You maybe at least would like to set testsession.testset[0] = {}; before accessing its members.

Comments

0

I think you are working code like this.

<script >

var testsession = {};
testsession.testset = [];
testsession.testset.push({testsetname:"week9"});
testsession.testset[0].list = [];

testsession.testset[0].list.push({distance:"400M"});
testsession.testset[0].list[0].athletes = [];
testsession.testset[0].list[0].athletes.push({ AthleteID:  "a92", Time: "49.2"});

testsession.testset[0].list.push({distance:"900M"});
testsession.testset[0].list[1].athletes = [];
testsession.testset[0].list[1].athletes.push({ AthleteID:  "a93", Time: "99.2"});

console.log(testsession);

</script>

and result will be like that:

"{"testset":[{"testsetname":"week9","list":[{"distance":"400M","athletes":[{"AthleteID":"a92","Time":"49.2"}]},{"distance":"900M","athletes":[{"AthleteID":"a93","Time":"99.2"}]}]}]}"

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.