Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Lodash uniqWith,怎么说Lodash保留最后一份副本_Javascript_Arrays_Object_Ecmascript 6_Lodash - Fatal编程技术网

Javascript Lodash uniqWith,怎么说Lodash保留最后一份副本

Javascript Lodash uniqWith,怎么说Lodash保留最后一份副本,javascript,arrays,object,ecmascript-6,lodash,Javascript,Arrays,Object,Ecmascript 6,Lodash,您好,我正在使用lodash uniqWith方法删除数组中具有相同id的重复项 但lodash保留了第一个重复项。 但我不想保留最后一件复制品 我能为此做些什么 var result = _.uniqWith(editedData, function(arrVal, othVal) { return arrVal.id === othVal.id; }); console.log(result) 最简单的方法?首先反转阵列(在克隆阵列以避免突变后) 您还可以简化代

您好,我正在使用lodash uniqWith方法删除数组中具有相同id的重复项

但lodash保留了第一个重复项。 但我不想保留最后一件复制品

我能为此做些什么

var result = _.uniqWith(editedData, function(arrVal, othVal) {
      return arrVal.id === othVal.id;
    });
    console.log(result)

最简单的方法?首先反转阵列(在克隆阵列以避免突变后)

您还可以简化代码:

var result = _.uniqWith(_.reverse(_.clone(editedData)), ({ id: a }, { id: b }) => a === b);

您可以使用
创建UniqByList函数。使用
.keyBy()
通过
id
获取对象,使用
.values()
获取数组:

const{flow,partiOK:pr,keyBy,values}=_
const lastUniqBy=iteratee=>flow(
pr(keyBy、iteratee),
价值观
)
常量arr=[{id:1,val:1},{id:1,val:2},{id:2,val:1},{id:2,val:2}]
const result=lastUniqBy('id')(arr)
console.log(结果)

reverse将变异原始阵列。reverse(u.clone(editedData))
better@OriDrori?它将变异克隆,而不是原始阵列,所以是的。你也可以使用spread获得浅层克隆。你救了我一天。谢谢:)反向操作的方法很好。没问题@user3348410,总是很乐意帮忙。
var result = _.uniqWith(_.reverse(_.clone(editedData)), ({ id: a }, { id: b }) => a === b);