更新现有的键值对会抛出<Object>没有方法'push'错误

Updating existing key value pair throws <Object> has no method 'push' error

本文关键字:Object 有方法 push 错误 键值对 更新      更新时间:2023-09-26

我想用一个新的键值更新一个现有的JavaScript对象,也更新一个现有的值,像这样

$.each(eventsData, function() {
    if (this.ID== "eb_0") {
        this.push({
            author_name: "John Seeds" //new key value pair to be added.
            title: newTitle_var //to be updated
        });
    }
});
console.log(this.eventsData)  

我的对象看起来像这样。

[
    Object { ID="eb_0", title="DayY", Date=Date, more...}, 
    Object { ID="eb_1", title="DayZ", Date=Date, more...},
    Object { ID="eb_2", title="DayX", Date=Date, more...}          
]

当前console.log输出如下错误:

Uncaught TypeError: Object #<Object> has no method 'push' 
有谁能帮我弄明白这个吗?

您可以使用:

$.each(eventsData, function () {
    if (this.ID == "eb_0") {
        this.author_name = "John Seeds"; //new key value pair to be added.
        this.title = newTitle_var; //to be updated
    }
});
相关文章: