嗨,我有一个具有相同UserId的数组,我想删除相似的数组并仅保留一个 我该怎么做?
示例:
a=[
{userId:1,name:''},
{userId:2,name:''},
{userId:3,name:''},
{userId:1,name:''},
{userId:1,name:''}
]
转化:
a=[
{userId:1,name:''},
{userId:2,name:''},
{userId:3,name:''}
]
答案 0 :(得分:2)
您可以使用过滤器:
a.filter((item, index, array) => {
const firstItemIndex = array.findIndex(innerItem => item.userId === innerItem.userId);
return firstItemIndex === index;
})
findIndex将始终返回其找到的第一个元素的索引。然后它将忽略重复项。
答案 1 :(得分:1)
这种代码的性能应该更好:
const keys = new Set<number>()
const result = []
for (const item of a) {
if (!keys.has(item.userId)) {
keys.add(item.userId)
result.push(item)
}
}
答案 2 :(得分:0)
我建议使用lodash uniqBy函数https://lodash.com/docs/4.17.15#uniqBy
>>> import numpy as np
... ang = np.array(
[[20, 10, 20, 10, 10, 20], [15, 10, 20, 22, 20, 32]],
dtype=np.float32
)
... fk = KinematicsPhysics()
... fk(ang)
<tf.Tensor: shape=(2, 6), dtype=float32, numpy=
array([[ 548.9287 , -118.2511 , -527.5551 , 47.538036, -30.621014, -144.55814 ],
[ 511.83527 , -85.72318 , -492.43365 , 49.052803, -46.48408 , -145.28882 ]], dtype=float32)>