Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用';这';在Javascript对象中_Javascript_Arrays_Object_Indexing - Fatal编程技术网

使用';这';在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>');               
        };
    }
}