Javascript:如何从重复的数组值中删除一个值

时间:2022-03-22 07:57:40

I have an array ['2530491','2530491','2530491','2530492'] the 2530491 is duplicated thrice, and I want to remove a single value of 2530491 from 3 of them, so the output would be like : ['2530491','2530491','2530492'].

我有一个数组['2530491' 2530491' 2530491' 2530491' 2530491' 2530491' 2530491' ' '2530491'是重复的三次,我想从其中的3个中去掉一个值2530491,所以输出会是:['2530491' 2530491'

fileArrayAnnounce_size = jQuery.grep(fileArrayAnnounce_size, function(value){
  return value != file_size.metas[0].size;
});

I try grip but it removes all value which same. I want to remove only a single value from duplicates.

我试着握住它,但它会移除所有相同的值。我只想从副本中删除一个值。

3 个解决方案

#1


2  

You could check if given element has dupe elements or not, if so - remove just one duplicate entry of specified element from the original array.

您可以检查给定的元素是否有dupe元素,如果有的话——从原始数组中删除指定元素的一个重复条目。

var arr = ['2530491','2530491','2530491','2530492'],
    hash = [...new Set(arr)];
    hash.forEach((v,i) => arr.indexOf(v) != arr.lastIndexOf(v) ? arr.splice(arr.indexOf(v), 1) : null);
    
    console.log(arr);

#2


3  

You can use splice and indexOf to remove the first instance:

您可以使用splice和indexOf来删除第一个实例:

fileArrayAnnounce_size.splice(fileArrayAnnounce_size.indexOf('2530491'), 1)

A safer way:

一个安全的方式:

var index = fileArrayAnnounce_size.indexOf('2530491')
if (index > -1) {
    fileArrayAnnounce_size.splice(index, 1);
}

Check and remove duplicates:

检查并删除重复:

var mapOfValues = fileArrayAnnounce_size.reduce(function(vals, current) {
    if (vals[current]) {
        vals[current]++;
    } else {
        vals[current] = 1;
    }

    return vals;
}, {});

And now check and remove anything with more than 1 value:

现在检查并删除任何值大于1的内容:

for (var value in mapOfValues) {
    if (mapOfValues[value] > 1) {
        var idx = fileArrayAnnounce_size.indexOf(value);
        fileArrayAnnounce_size.splice(idx, 1);
    }
}

Demo: https://jsfiddle.net/1277mxt9/

演示:https://jsfiddle.net/1277mxt9/

#3


1  

You can use filter() and pass one object as thisArg parameter to use it as hash table.

您可以使用filter()并将一个对象作为thisArg参数传递给它作为散列表。

var data = ['2530491','2530491','2530491','2530492', '2530492'];

var result = data.filter(function(e) {
  if(!this[e]) this[e] = 1
  else if (this[e] == 1) return this[e] = 2, false
  return true;
}, {})

console.log(result)

#1


2  

You could check if given element has dupe elements or not, if so - remove just one duplicate entry of specified element from the original array.

您可以检查给定的元素是否有dupe元素,如果有的话——从原始数组中删除指定元素的一个重复条目。

var arr = ['2530491','2530491','2530491','2530492'],
    hash = [...new Set(arr)];
    hash.forEach((v,i) => arr.indexOf(v) != arr.lastIndexOf(v) ? arr.splice(arr.indexOf(v), 1) : null);
    
    console.log(arr);

#2


3  

You can use splice and indexOf to remove the first instance:

您可以使用splice和indexOf来删除第一个实例:

fileArrayAnnounce_size.splice(fileArrayAnnounce_size.indexOf('2530491'), 1)

A safer way:

一个安全的方式:

var index = fileArrayAnnounce_size.indexOf('2530491')
if (index > -1) {
    fileArrayAnnounce_size.splice(index, 1);
}

Check and remove duplicates:

检查并删除重复:

var mapOfValues = fileArrayAnnounce_size.reduce(function(vals, current) {
    if (vals[current]) {
        vals[current]++;
    } else {
        vals[current] = 1;
    }

    return vals;
}, {});

And now check and remove anything with more than 1 value:

现在检查并删除任何值大于1的内容:

for (var value in mapOfValues) {
    if (mapOfValues[value] > 1) {
        var idx = fileArrayAnnounce_size.indexOf(value);
        fileArrayAnnounce_size.splice(idx, 1);
    }
}

Demo: https://jsfiddle.net/1277mxt9/

演示:https://jsfiddle.net/1277mxt9/

#3


1  

You can use filter() and pass one object as thisArg parameter to use it as hash table.

您可以使用filter()并将一个对象作为thisArg参数传递给它作为散列表。

var data = ['2530491','2530491','2530491','2530492', '2530492'];

var result = data.filter(function(e) {
  if(!this[e]) this[e] = 1
  else if (this[e] == 1) return this[e] = 2, false
  return true;
}, {})

console.log(result)