Home javascript Remove element from js array

Remove element from js array

Author

Date

Category

There is an array and a function, you need to delete elements from the array, I understand that delete deletes an element, but after it there is a “hole”, how can I completely delete it?

var myArr = [1, 2, 3, 6, 7, 13, 349, 3483 , 5432];
var doubledOdds = onlyDoubledOdds (myArr);
console.log (doubledOdds.length);
console.log (doubledOdds);
if (doubledOdds === myArr) {
 console.log ('WTF ?!');
} else {
 console.log ('Ok');
}
function onlyDoubledOdds (sourceArray) {
 var result = sourceArray;
 for (var i = 0; i & lt; result.length; i ++) {
  if (i% 2 == 0) {
   delete result [i];
  } else {
   result [i] * = 2;
  }
 }
 return result;
} 

Answer 1, authority 100%

Special methods can be used to remove it, such as

  • shift – removes from the beginning
  • pop – from the end
  • splice – from the middle

With the help of splice , you can just delete elements, insert elements, replace elements – one at a time and at the same time.

The syntax is:
arr.splice (start [ countDelete [ elemInsert1, …, elemInsertN]])

i.e. delete countDelete elements starting at start and then insert elemInsert1, ..., elemInsertN in their place. If countDelete is not specified, then everything from start to the end will be cut off. Returns an array of removed elements.

An example with deletion looks like this:

var arr = ["Hello", "you", "Misha"];
arr.splice (1, 1); // starting from position 1, remove 1 element
alert (arr); // left ["Hello", "Misha"]

and substitution:

var arr = ["Hello", "you", "boy", "Misha"];
// remove the first 3 items and add others instead
arr.splice (0, 3, "Zdarof!")
alert (arr) // now ["Zdarof!", "Misha"]

Answer 2, authority 36%

When using the delete operator, the length property of the array does not change, so when using the length in a loop, there will be no difference.

The onlyDoubledOdds function does something incomprehensible, depending on the index of the element, and changes the original array, therefore, when comparing, the returned value is naturally equal to the passed one, and not "WTF"

If you need to filter an array, you can use the filter function ,
if you need to get an array based on an existing one, but with different elements, you can use the map ,
if you want in one pass, you can use the function reduce

For example:

var myArr = [1, 2, 3, 6, 7, 13, 349, 3483 , 5432];
var doubledOddsMapFilter = onlyDoubledOddsMapFilter (myArr);
console.log ('doubledOddsMapFilter.length', doubledOddsMapFilter.length);
console.log ('doubledOddsMapFilter', doubledOddsMapFilter);
if (doubledOddsMapFilter === myArr) {
 console.log ('WTF ?!');
} else {
 console.log ('Ok');
}
var doubledOddsReduce = onlyDoubledOddsReduce (myArr);
console.log ('doubledOddsReduce.length', doubledOddsReduce.length);
console.log ('doubledOddsReduce', doubledOddsReduce);
if (doubledOddsReduce === myArr) {
 console.log ('WTF ?!');
} else {
 console.log ('Ok');
}
function onlyDoubledOddsMapFilter (sourceArray) {
 return sourceArray.filter ((el, i) = & gt; i% 2! = 0) .map (el = & gt; el * 2);
}
function onlyDoubledOddsReduce (sourceArray) {
 return sourceArray.reduce ((acc, el, i) = & gt; {
  if (i% 2! = 0) acc.push (el * 2);
  return acc;
 }, []);
} 

Answer 3

var myArr = [1, 2, 3, 6, 7, 13, 349, 3483, 5432];
myArr = myArr.map (el = & gt; el% 2! == 0? el * 2: el);
myArr = myArr.filter (el = & gt; el% 2 === 0? true: false);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions