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
我不断得到TypeError:testsession.testset [0] .athletes未定义 - 我尝试了很多不同的方法,是不是有一个对象数组的数组
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 个解决方案
#1
1
The testset[0] is a string. Make it an object
testset [0]是一个字符串。把它变成一个对象
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]);
#2
1
testsession.testset[0]
is a primitive value, a string.
testsession.testset [0]是一个原始值,一个字符串。
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.
这里发生了什么?左边的原语没有运动员属性,但JavaScript会将它强制转换为String对象,然后将该属性分配给该临时String对象,然后该对象消失为遗忘。
So it is like that assignment never happened: testsession.testset[0]
will remain a primitive value, and primitive values have no properties.
所以它就像从未发生过一样:testsession.testset [0]将保持原始值,而原始值没有属性。
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
.
当您阅读运动员属性时,同样会再次发生:JavaScript将其强制转换为String对象,只发现该对象没有运动员属性,因此您未定义。
#3
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.
当您尝试访问testsession.testset [0]时,该条目是一个字符串。你可能至少想设置testsession.testset [0] = {};在访问其成员之前。
#4
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"}]}]}]}"
#1
1
The testset[0] is a string. Make it an object
testset [0]是一个字符串。把它变成一个对象
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]);
#2
1
testsession.testset[0]
is a primitive value, a string.
testsession.testset [0]是一个原始值,一个字符串。
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.
这里发生了什么?左边的原语没有运动员属性,但JavaScript会将它强制转换为String对象,然后将该属性分配给该临时String对象,然后该对象消失为遗忘。
So it is like that assignment never happened: testsession.testset[0]
will remain a primitive value, and primitive values have no properties.
所以它就像从未发生过一样:testsession.testset [0]将保持原始值,而原始值没有属性。
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
.
当您阅读运动员属性时,同样会再次发生:JavaScript将其强制转换为String对象,只发现该对象没有运动员属性,因此您未定义。
#3
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.
当您尝试访问testsession.testset [0]时,该条目是一个字符串。你可能至少想设置testsession.testset [0] = {};在访问其成员之前。
#4
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"}]}]}]}"