使用';这';在Javascript对象中
在让“这个”按我预期的方式运行时遇到了一些困难- 基本上,我有一个对象,我无法从同一对象中的函数访问对象中的数组- 看起来是这样的:使用';这';在Javascript对象中,javascript,arrays,object,indexing,Javascript,Arrays,Object,Indexing,在让“这个”按我预期的方式运行时遇到了一些困难- 基本上,我有一个对象,我无法从同一对象中的函数访问对象中的数组- 看起来是这样的: var articles = { article : { 1: { title : 'This is a Title', content : 'This is the content of the article' }, 2: { tit
var articles = {
article : {
1: {
title : 'This is a Title',
content : 'This is the content of the article'
},
2: {
title : 'This is the second article',
content : 'This is the second article content'
},
3: {
title : 'This is the third article',
content : 'Making information transferrable. Identifiable. Manipulatable.'
}
},
numArticles : function(){
var size = 0, key;
for (key in this.article){
if(this.article.hasOwnProperty(key)) size++;
}
return size;
},
buildInterface : function(){
var aSize = this.numArticles();
for(var i = 0; i < aSize; i++){
$('body').append('<article><h2>' + this.article[i].title + '</h2></article>');
}
}
}
var文章={
第条:{
1: {
标题:“这是一个标题”,
内容:“这是文章的内容”
},
2: {
标题:"这是第二篇文章",,
内容:“这是第二篇文章的内容”
},
3: {
标题:"这是第三篇文章",,
内容:“使信息可传递、可识别、可操作。”
}
},
numArticles:function(){
变量大小=0,键;
for(在本文中输入){
if(this.article.hasOwnProperty(key))size++;
}
返回大小;
},
buildInterface:function(){
var aSize=this.numArticles();
对于(变量i=0;i
这里的任何帮助都将不胜感激-
我有一种预感,这可能是一个范围问题——希望它与JSFIDLE无关-
非常感谢-
和平
标记您的文章变量的索引不一致:属性是从1
开始定义的,而在buildArticles
方法for
循环中从0
开始定义的。您可以使用
for(var i = 1; i <= aSize; i++){
$('body').append('<article><h2>' + this.article[i].title + '</h2></article>');
};
…让您的buildArticles保持现在的循环状态(因为索引现在正确地从0开始)
顺便说一句,这样你甚至不需要做一个特殊的函数来计算你的文章:article.length
就足够了
下面是此方法的示例
作为旁注,如果您确实检查了调试器,您会注意到它是this.articles[0]
未定义的(因此试图从中删除title
是错误的),而不是this.articles
。因此,这绝对不是范围问题。这应该可以:
var articles = {
article : {
1: {
title : 'This is a Title',
content : 'This is the content of the article'
},
2: {
title : 'This is the second article',
content : 'This is the second article content'
},
3: {
title : 'This is the third article',
content : 'Making information transferrable. Identifiable. Manipulatable.'
}
},
numArticles : function(){
var size = 0, key;
for (key in this.article){
if(this.article.hasOwnProperty(key)) size++;
}
return size;
},
buildInterface : function(){
var aSize = this.numArticles();
for(var i = 1; i <= aSize; i++){
console.log( '<article><h2>' + this.article[i]['title'] + '</h2></article>');
};
}
}
var文章={
第条:{
1: {
标题:“这是一个标题”,
内容:“这是文章的内容”
},
2: {
标题:"这是第二篇文章",,
内容:“这是第二篇文章的内容”
},
3: {
标题:"这是第三篇文章",,
内容:“使信息可传递、可识别、可操作。”
}
},
numArticles:function(){
变量大小=0,键;
for(在本文中输入){
if(this.article.hasOwnProperty(key))size++;
}
返回大小;
},
buildInterface:function(){
var aSize=this.numArticles();
对于(var i=1;i@thatidotguy,它是变量“articles”…@Pointy:我想那个白痴的意思是“我没有看到数组”:p j/k@jAndy为我的愚蠢道歉。不过我确实有这个名字!@thatidiotguy:哈哈,是的,我只是在开玩笑,看起来也很像一个打字错误,既然你把文章
当作一个数组,为什么不把它做成一个真正的数组呢?你可以免费得到长度
,这样你就不需要numArticles了。好吧,酷-我不会给你他打了一枪-谢谢你的反馈-关于正确的阵列提示的很棒的建议-肯定会切换到这一点-
var articles = {
article : {
1: {
title : 'This is a Title',
content : 'This is the content of the article'
},
2: {
title : 'This is the second article',
content : 'This is the second article content'
},
3: {
title : 'This is the third article',
content : 'Making information transferrable. Identifiable. Manipulatable.'
}
},
numArticles : function(){
var size = 0, key;
for (key in this.article){
if(this.article.hasOwnProperty(key)) size++;
}
return size;
},
buildInterface : function(){
var aSize = this.numArticles();
for(var i = 1; i <= aSize; i++){
console.log( '<article><h2>' + this.article[i]['title'] + '</h2></article>');
};
}
}