简体   繁体   English

如何使用lodash从数组中删除对象?

[英]How to remove an object from an array using lodash?

I have a variable that looks something like this 我有一个看起来像这样的变量

[ { url : "http:image.gif"}, { url : "http:image.gif"} , { url : "http:image.gif"}]

I would like a function that goes through and removes one object each time the function is called until there are no more items left. 我想要一个在每次调用该函数时都要经过并删除一个对象的函数,直到没有剩余的项目为止。

something like 就像是

function Decrement(){

    if array is not empty

        var lastKey = _.last (object);  

        var updatedObject = _.without ( object, lastkey); 

        return the last key of the new updatedObject

} 

You could use a combination of JavaScript's pop and lodash's _.last : 您可以结合使用JavaScript的pop和lodash的_.last

function decrement(array) {
  array.pop();
  return _.last(array);
}

 var a = [1, 2, 3]; function decrement(array) { array.pop(); return _.last(array); } var result = decrement(a); console.log(result, a); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM