_.remove的反义词是什么

What is the opposite of _.remove

    _.remove($scope.posts, post);

I',使用 _.remove 使用 lodash 从数组中删除项目。但是如何再次添加对象呢?那么_.remove.

的反义词是什么

请尝试_.fill,用从开始到

的值填充数组元素

_.fill(array, value, [start=0], [end=array.length])

_.fill([4, 6, 8, 10], '*', 1, 3);
// → [4, '*', '*', 10]

_.remove 从数组中删除一个项目,现在因为你想要一个可以推送的删除的对立面,所以没有 _.push 可用。所以,我觉得还是用原生推送功能比较好。 以下是您可以考虑的几件事:

var posts = [{a:1},{b:3},{f:3}];
var post = {a:1};

_.remove(posts, post); // posts = [{b:3},{f:3}]

在 0 索引处添加对象

posts.unshift(post);//posts = [{a:1},{b:3},{f:3}]

在最后一个索引处添加对象

posts.push(post);//posts = [{b:3},{f:3},{a:1}]

在索引处插入对象

posts.splice(1, 0, {g:8}); // posts = [{a:1},{g:8},{b:3},{f:3}]

你当然可以使用 _.mixin。

 _.mixin({
   push: function(arr,obj){
       return arr.push(obj);
   }
 }); 

您可以像

一样使用它
 _.push(posts,post);

JsFiddle for mixin example