Home > Blockchain >  How to get selected values in an array and pass the others to a new array
How to get selected values in an array and pass the others to a new array

Time:07-24

Copy the uncorrupted data in the array stored in targetDisk to the empty array newDisk (corrupted data looks like this: ø). Then print the contents of the disk to the terminal as a string.

var targetDisk = [ 'E', 'ø', '-', 'C', 'ø', 'o', 'r', 'ø', 'ø', 'p', '\'', 'ø', 's', ' ', 'E', 'v', 'ø', 'i', 'ø', 'ø', 'l'];
var newDisk = [];
var corruptionSymbol = 'ø';


for ( i=0; i < targetDisk.length; i  ;){


if (targetDisk[i] !== corruptionSymbol);{

      newDisk.push (targetDisk[i]);
   }
 }

CodePudding user response:

var targetDisk = [ 'E', 'ø', '-', 'C', 'ø', 'o', 'r', 'ø', 'ø', 'p', '\'', 'ø', 's', ' ', 'E', 'v', 'ø', 'i', 'ø', 'ø', 'l'];
var corruptionSymbol = 'ø';
var newDisk = [];

for (var i = 0; i < targetDisk.length; i  = 1) {
  if (targetDisk[i] !== corruptionSymbol) {
    newDisk.push(targetDisk[i]);
  }
}

console.log(newDisk);

CodePudding user response:

You can simply achieve it by using Array.filter()

Live Demo :

let targetDisk = [ 'E', 'ø', '-', 'C', 'ø', 'o', 'r', 'ø', 'ø', 'p', '\'', 'ø', 's', ' ', 'E', 'v', 'ø', 'i', 'ø', 'ø', 'l'];

let corruptionSymbol = 'ø';

const newDisk = targetDisk.filter(item => item !== corruptionSymbol);

console.log(newDisk);

  • Related