Javascript 更新集合中的值

Javascript 更新集合中的值,javascript,ractivejs,Javascript,Ractivejs,我收集了一些帖子作为数据: var ractive = new Ractive({ el: '#templateContainer', template: '#template', data: { Posts: [{"Id":"posts/97", Content="Blog post test", Date="Something"}, ...]; } }); 在某个时刻,我收到一个通知,说明博客文章内容已更改: funcion onBlogPo

我收集了一些帖子作为数据:

var ractive = new Ractive({
    el: '#templateContainer',
    template: '#template',
    data: {
        Posts: [{"Id":"posts/97", Content="Blog post test", Date="Something"}, ...];
    }
});
在某个时刻,我收到一个通知,说明博客文章内容已更改:

funcion onBlogPostContentChanged(postId, newContent) {
    ractive.set(..., newContent);
}

问题是我不知道如何指定
ractive.set
,以便更改具有特定Id的博客文章的内容。

您可以通过数组索引和属性设置键路径:

function onItemChanged(id, newContet) {
    var posts = r.get('Posts'), index = -1

    for(var i = 0; i < posts.length; i++){
        if(post[i].Id === id){
            index = i
            break
        }        
    }

    if(index !== -1){
        r.set('Posts.' + index + '.Content', newContent)
    }

    // or if using "magic: true"**
    Posts[index].Content = newContent
}
函数已更改(id,newContet){
var posts=r.get('posts'),index=-1
对于(变量i=0;i
有关工作示例,请参见


**

是整个帖子的
newContent
吗?或者只是要更新的数组中post对象的属性?@martypdx,它是类似“Posts”的属性项。+索引不起作用。尝试了“Posts['+index+']”,但它也不起作用。更新后,请查看JSFIDLE示例。谢谢,它起作用了。正确的语法是Posts.index而不是Posts[index],这有点奇怪。用于get和set的字符串是。我添加了一个使用纯javascript的示例,它在IE8上运行
magic:true