是否有可能通過它的值從數組中除去對象,而不是通過沒有使用for
循環進行迭代的索引?Vue:如何從數組中刪除對象的值?
我試圖用重複刪除元素,但它看起來像它不刪除重複的元素:
App.$refs.userContent.foo : [1,2,3,4]
console.log(App.$refs.userContent.foo);
App.$refs.userContent.foo.forEach(function(x,i)
{
App.$refs.userContent.foo.$remove(i);
});
console.log(App.$refs.userContent.foo);
結果:
[1, 2, 3, 4, __ob__: Observer]
[3, 4, __ob__: Observer]
爲什麼它的做不刪除所有元素?
它是不錯的代碼?對於(i = 0; i <= z; i ++) ' var z = App. $ refs.userContent.foo.length {.refs.userContent.foo。$ remove(i); }'? –
不,如果你真的想用這種方法清除數組,你需要向後循環:for(i = App。$ refs.userContent.foo.length - 1; i> = 0; i--){ –