从数组中删除重复项,但使用JS忽略一个参数

时间:2019-03-29 10:45:38

标签: javascript arrays

如何获取唯一数组,却忽略数组中的参数之一?我想忽略name并与其他人进行比较,比如说...

let inventories = [
  {name: 'Polo Shirt', size: 'XL', price: 19.90, color: 'black'},
  {name: 'Polo Shirt (Stripe)', size: 'XL', price: 19.90, color: 'black'},
  {name: 'Polo Shirt (Printed)', size: 'XL', price: 19.90, color: 'black'},
  {name: 'Short Sleeve Polo Shirt', size: 'L', price: 14.90, color: 'blue'},
  {name: 'Sleeve Polo Shirt', size: 'L', price: 20.90, color: 'black'},
  {name: 'Long Sleeve Polo Shirt', size: 'L', price: 14.90, color: 'blue'},
  {name: 'Long Sleeve Polo Shirt', size: 'L', price: 14.90, color: 'blue'},
  {name: 'Long Sleeve Polo Shirt', size: 'L', price: 14.90, color: 'blue'},
];

我想要的结果:

[
  {name: 'Polo Shirt (Printed)', size: 'XL', price: 19.90, color: 'black'},
  {name: 'Sleeve Polo Shirt', size: 'L', price: 20.90, color: 'black'},
  {name: 'Long Sleeve Polo Shirt', size: 'L', price: 14.90, color: 'blue'},
];

我知道我们可以通过以下方式获得唯一数组:

inventories = inventories
  .map(JSON.stringify)
  .reverse()
  .filter(function(item, index, inventories) {
    return inventories.indexOf(item, index + 1) === -1;

但是我不确定如何告诉他们忽略name参数...

3 个答案:

答案 0 :(得分:3)

我认为,这是最简单的答案。您可以创建哈希表并根据login = () => { fetch('url', { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', 'Connection': 'Keep-Alive', }, credentials: 'include', body: JSON.stringify({ username: this.state.email, password: this.state.password, }) }) 以外的属性生成唯一键。请参见以下代码:

name
let inventories = [
  {name: 'Polo Shirt', size: 'XL', price: 19.90, color: 'black'},
  {name: 'Polo Shirt (Stripe)', size: 'XL', price: 19.90, color: 'black'},
  {name: 'Polo Shirt (Printed)', size: 'XL', price: 19.90, color: 'black'},
  {name: 'Short Sleeve Polo Shirt', size: 'L', price: 14.90, color: 'blue'},
  {name: 'Sleeve Polo Shirt', size: 'L', price: 20.90, color: 'black'},
  {name: 'Long Sleeve Polo Shirt', size: 'L', price: 14.90, color: 'blue'},
  {name: 'Long Sleeve Polo Shirt', size: 'L', price: 14.90, color: 'blue'},
  {name: 'Long Sleeve Polo Shirt', size: 'L', price: 14.90, color: 'blue'}
]

const hash = {}

inventories.forEach(function(elem) {
  var selected =  Object.assign({}, elem);
  delete selected.name;
  const key = Object.values(selected).join('-');
  hash[key] = elem;
});
const result = Object.values(hash);

console.log(result);

答案 1 :(得分:1)

您可以将filter的{​​{1}}方法与rest参数设为Object.values

...

答案 2 :(得分:1)

您可以将Map.reduce一起使用,并利用以下事实:它将覆盖具有相同key的任何元素集,如下所示:

const inventories = [{"name":"Polo Shirt","size":"XL","price":19.9,"color":"black"},{"name":"Polo Shirt (Stripe)","size":"XL","price":19.9,"color":"black"},{"name":"Polo Shirt (Printed)","size":"XL","price":19.9,"color":"black"},{"name":"Short Sleeve Polo Shirt","size":"L","price":14.9,"color":"blue"},{"name":"Sleeve Polo Shirt","size":"L","price":20.9,"color":"black"},{"name":"Long Sleeve Polo Shirt","size":"L","price":14.9,"color":"blue"},{"name":"Long Sleeve Polo Shirt","size":"L","price":14.9,"color":"blue"},{"name":"Long Sleeve Polo Shirt","size":"L","price":14.9,"color":"blue"}];

const res = [...inventories.reduce((acc, obj) => {
  const {name, ...others} = obj;
  const unq_key = Object.values(others).join('_');
  acc.set(unq_key, obj);
  return acc;
}, new Map).values()];

console.log(res);