简体   繁体   English

如何将对象推入JavaScript Prototype内部的数组?

[英]How to push objects into array inside of a JavaScript Prototype?

I have an Angular Factory whose job is to save special objects and retrieve them later. 我有一个Angular Factory,其工作是保存特殊对象并在以后检索它们。
(Used to save the users workflow when he switches views). (用于在切换视图时保存用户工作流程)。

Basically I need to save objects with a name, as well as an array of tags. 基本上我需要保存带有名称的对象以及一组标签。 My problem is with the saving the tags into the array part. 我的问题是将标签保存到数组部分。

The Prototype constructor: Prototype构造函数:

// define the TagsObject constructor function
var TagsObject = function(name, tag) {
    this.name = name;
    this.tags = [].push(tag);
    return this;
};

Full factory code: 完整的工厂代码:

.factory('TagFactory', [function() {

    // Init TagFactory:
    var tagContainers = [];

    // define the TagsObject constructor function
    var TagsObject = function(name, tag) {
        this.name = name;
        this.tags = [].push(tag);
        return this;
    };

    console.log('tagFactory');
    console.log(TagsObject);

    var saveTags = function(name, tag) {

        tagContainers.push(new TagsObject(name, tag));

        console.log('saveTags:');
        console.log(tagContainers);
    };

    var getTags = function(name) {
        console.log(name);
        return this;
    };

    return {
        saveTags : saveTags,
        getTags  : getTags
    };
}]);

In a different controller I now save some data into a new Prototype inside of the TagFactory: 在不同的控制器中,我现在将一些数据保存到TagFactory内的new Prototype中:

TagFactory.saveTags('TEST', tagObj);

Now back in my Factory, where you see the console.log(nameTagContainers); 现在回到我的工厂,在那里你可以看到console.log(nameTagContainers); the following is the log: 以下是日志:

[TagsObject]
    0: TagsObject
    tags: 1
    name: "TEST"
    __proto__: TagsObject
    length: 1
    __proto__: Array[0]

^ tags is an Array, but it's showing 1, instead of the Object details... do you see where I went wrong? ^标签是一个数组,但它显示1,而不是对象详细信息...你看到我哪里出错了吗?



UPDATE: This question was answered below by Kauê Gimenes , however I'm sharing the extra code I added to fix my other problem. 更新: KauêGimenes在下面回答了这个问题,但我正在分享我为解决其他问题而添加的额外代码。

Which was every time a new tag was selected for a currently selected name, it generated a new Prototype, instead of saving the tag into an existing Prototype: 每当为当前选择的名称选择新标签时,它就会生成一个新的Prototype,而不是将标签保存到现有的Prototype中:

var saveTags = function(name, tag) {

    console.log(tagContainers.length);

    if (tagContainers.length != 0) {
        for(var i = 0; i < tagContainers.length; i++) {
            if (tagContainers[i].name == name) {
                console.log('Found existing name! Add tag to existing obj');
                tagContainers[i].tags.push(tag);
                break;
            } else {
                console.log('New name, create new obj');
                tagContainers.push(new TagsObject(name, tag));
            }
        }
    }
    else {
        console.log('New name, init: create the first obj');
        tagContainers.push(new TagsObject(name, tag));
    }

    console.log(' ');
    console.log('tagContainers:');
    console.log(tagContainers);
};

Try this approach: 试试这种方法:

// define the TagsObject constructor function
var TagsObject = function(name, tag) {
    this.name = name;
    this.tags = [tag];
    return this;
};

The best way to initialize an array is to include the elements inside the brackets 初始化数组的最佳方法是在括号内包含元素

[ 1 , 2 , 3 ]

But in case you want to initialize with n elements you could use the concat function. 但是如果你想用n个元素初始化,你可以使用concat函数。

[ 1 ].concat([ 2 , 3 , 4 ])

The result would be: 结果将是:

[ 1 , 2 , 3 , 4 ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM