admin管理员组

文章数量:1291503

I'm trying to only remove one of the 2s from an array, but my code removes all of them. My code is as follows:

var arr = [2,7,9,5,2]
arr.filter(item => ((item !== 2)));

and:

var arr = [2,7,9,2,2,5,2]
arr.filter(item => ((item !== 2)));

Both remove all the 2s. I thought about removing duplicates, where it works if there's only one duplicate - e.g.:

Array.from(new Set([2,7,9,5,2]));
function uniq(a) {
 return Array.from(new Set(a)) 
}

But fails if there's multiple duplicates as it just removes them all, including any other duplicated numbers:

Array.from(new Set([2,7,9,9,2,2,5,2]));
function uniq(a) {
 return Array.from(new Set(a)) 
}

Does anyone know how to only remove one of the 2s? Thanks for any help here.

I'm trying to only remove one of the 2s from an array, but my code removes all of them. My code is as follows:

var arr = [2,7,9,5,2]
arr.filter(item => ((item !== 2)));

and:

var arr = [2,7,9,2,2,5,2]
arr.filter(item => ((item !== 2)));

Both remove all the 2s. I thought about removing duplicates, where it works if there's only one duplicate - e.g.:

Array.from(new Set([2,7,9,5,2]));
function uniq(a) {
 return Array.from(new Set(a)) 
}

But fails if there's multiple duplicates as it just removes them all, including any other duplicated numbers:

Array.from(new Set([2,7,9,9,2,2,5,2]));
function uniq(a) {
 return Array.from(new Set(a)) 
}

Does anyone know how to only remove one of the 2s? Thanks for any help here.

Share asked Nov 19, 2018 at 10:32 user8758206user8758206 2,1917 gold badges27 silver badges75 bronze badges 3
  • 1 create a simple loop, when found an index with value of 2: .splice(idx, 1) this index and break the loop – Calvin Nunes Commented Nov 19, 2018 at 10:34
  • 1 Which one do you want to remove ? The first 2, or last, or ... – Mihai Alexandru-Ionut Commented Nov 19, 2018 at 10:34
  • 1 Possible duplicate of How do I remove a particular element from an array in JavaScript? – Haroldo_OK Commented Nov 19, 2018 at 10:38
Add a ment  | 

6 Answers 6

Reset to default 6

You could use indexOf method in bination with splice.

var arr = [2,7,9,5,2]
var idx = arr.indexOf(2)
if (idx >= 0) {
    arr.splice(idx, 1);
}
console.log(arr);

You could take a closure with a counter and remove only the first 2.

var array = [2, 7, 9, 2, 3, 2, 5, 2],
    result = array.filter((i => v => v !== 2 || --i)(1));
    
console.log(result);

For any other 2, you could adjust the start value for decrementing.

var array = [2, 7, 9, 2, 3, 2, 5, 2],
    result = array.filter((i => v => v !== 2 || --i)(2));
    
console.log(result);

There are various ways to do that; one relatively simple way would be to use indexOf; see this other post: https://stackoverflow./a/5767357/679240

var array = [2, 7, 9, 5, 2];
console.log(array)
var index = array.indexOf(2);
if (index > -1) {
  array.splice(index, 1);
}
// array = [7, 9, 5, 2]
console.log(array);

you can follow the following method

var arr= [2,3,4,2,4,5];
var unique = [];
$.each(arr, function(i, el){
    if($.inArray(el, unique) === -1) unique.push(el);
})

You can do:

const arr = [2, 7, 9, 2, 2, 5, 2];
const result = arr
  .reduce((a, c) => {
    a.temp[c] = ++a.temp[c] || 1;
    if (a.temp[c] !== 2) {
      a.array.push(c);
    }
    return a;
  }, {temp: {}, array: []})
  .array;

console.log(result);

Most simple way to filter all duplicates from array:

arr.filter((item, position) => arr.indexOf(item) === position)

This method skip element if another element with the same value already exist.

If you need to filter only first duplicate, you can use additional bool key:

arr.filter((item, position) => {
  if (!already && arr.indexOf(item) !== position) {
    already = true
    return false
  } else return true
})

But this method have overheaded. Smartest way is use for loop:

for (let i = 0; i < arr.length; i++) {
  if (arr.indexOf(arr[i]) !== i) {
    arr.splice(i,1);
    break;
  }
}

本文标签: javascriptRemove Only One Duplicate from An ArrayStack Overflow