-3

window.arrayone = ["1","2","3"];
window.arraytwo = ["1","1","1"];

arrayone.splice(arrayone.indexOf("1",-1));
arraytwo.splice(arraytwo.indexOf("1",-1));

console.log("arrayone -->" + arrayone);
console.log("arraytwo -->" + arraytwo);

How can I delete only one item. I have a number of items with the same attribute. I am trying to delete only one item. I.e array ["1", "1", "1"] Array.splice (array.indexOf (1.1)) Now if I have another set of items. The function does work and subtracts item 1. But when there is a group with similar items it does not work. Can someone tell me?

Answer: Can I close I realized my mistake. DeleteCount should be -1 negative. And not as positive as I had in the code.

        JAVA SCRIPT

      window.arrayone = ["1","2","3"];
window.arraytwo = ["1","1","1"];

arrayone.splice(arrayone.indexOf("1",1));
arraytwo.splice(arraytwo.indexOf("1",1));

alert("arrayone -->" + arrayone);
alert("arraytwo -->" + arraytwo);
  • Possible duplicate of [Javascript: How to remove only one value from duplicate array values](https://stackoverflow.com/questions/43522191/javascript-how-to-remove-only-one-value-from-duplicate-array-values) – Sandman May 29 '17 at 15:32

2 Answers2

1

Splice can take two arguments. The position to start at and the number of elements to remove. If you only want one element removed then include splice(startIndex, 1)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice?v=control

var test = [ 1, 2, 3, 3, 3, 3, 4, 5, 6, 7 ];

console.log(test.toString());

test.splice(test.indexOf(3), 1);

console.log(test.toString());
Taplar
  • 24,788
  • 4
  • 22
  • 35
0

Answer: Can I close I realized my mistake. DeleteCount should be -1 negative. And not as positive as I had in the code.

window.arrayone = ["1","2","3"]; window.arraytwo = ["1","1","1"]; arrayone.splice(arrayone.indexOf("1",-1)); arraytwo.splice(arraytwo.indexOf("1",-1)); console.log("arrayone -->" + arrayone); console.log("arraytwo -->" + arraytwo);