Array of objects on a json

This question already has an answer here:

  • How to append something to an array? 25 answers

  • 只需将新项目推送至历史数组:

    this.disputa.historico.push({
    fl_usuario: "",
                 created_at: "",
                 updated_at: "",
                 created_by: null,
                 updated_by: null,
                 texto: ""
    });
    

    只需追加一个数组:

    this.disputa.historico.push({/* your object here*/});
    

    You run into the error

    Cannot set property 'texto' of undefined

    when you try to access a property on a undefined or null object.

    You will first have to create a empty object in its place, if not already existing. The way your code is, it is expecting the object to be present in the index you are trying to access.

    var existingObj = this.disputa.historico[this.i];
    
    // if the value is falsy, then go ahead and create
    // an empty object at that index
    if(!existingObj) {
       this.disputa.historico[this.i] = {};
    }
    
    existingObj.texto = "something";
    
    链接地址: http://www.djcxy.com/p/17944.html

    上一篇: 如何将数字添加到JavaScript中的数组?

    下一篇: json上的对象数组