admin管理员组

文章数量:1326278

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'].

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.

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'].

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.

Share Improve this question edited Apr 20, 2017 at 15:08 kind user 41.9k8 gold badges68 silver badges78 bronze badges asked Apr 20, 2017 at 14:20 HiFlyer OjtHiFlyer Ojt 311 silver badge8 bronze badges 8
  • stackoverflow./questions/18008025/… – SKARVA Bodavula Commented Apr 20, 2017 at 14:25
  • what are the rules exactly....allow 2 duplicates but no more? – charlietfl Commented Apr 20, 2017 at 14:26
  • @charlietfl that seems to be the case. Twice but not thrice. – evolutionxbox Commented Apr 20, 2017 at 14:27
  • Even more than 2 duplicates as long as I removed a single of it. Just to remove single value from duplicate regardless how many duplicates – HiFlyer Ojt Commented Apr 20, 2017 at 14:28
  • So if there are four of the same, you still only wan't to remove one item? May you clarify the rules? – evolutionxbox Commented Apr 20, 2017 at 14:28
 |  Show 3 more ments

3 Answers 3

Reset to default 5

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

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:

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

Demo: https://jsfiddle/1277mxt9/

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.

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);

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

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)

本文标签: jqueryJavascript How to remove only one value from duplicate array valuesStack Overflow