-1

I have an array, its length can be up to 20 elements:

[ '0ad39f02478f5;',
  '309c9e9051482;',
  'dd7d5faf07569;',
  'ghdg9f02478f5;',
  '30967uj051482;',
  'ad398ixc478f5;',
  '39c9frtuyr482;' ]

I need to remove the semicolon from the last element in the array.

Here is the result I expect:

[ '0ad39f02478f5;',
  '309c9e9051482;',
  'dd7d5faf07569;',
  'ghdg9f02478f5;',
  '30967uj051482;',
  'ad398ixc478f5;',
  '39c9frtuyr482' ]
for (const value of Object.values(arrayOfJobs)) {
    console.log(value)
    const arrayLength = value.length - 1;
    value[arrayLength].replace(';', '');
    console.log(value)
}

I tried to do it like this, but it still doesn’t remove the semicolon at the end of the array. My problem is that the value is not assigned

MegaRoks
  • 898
  • 2
  • 13
  • 30

6 Answers6

5

You could pop the item, slice the string and push the item to the end of the array.

var array = ['0ad39f02478f5;', '309c9e9051482;', 'dd7d5faf07569;', 'ghdg9f02478f5;', '30967uj051482;', 'ad398ixc478f5;', '39c9frtuyr482;']

array.push(array.pop().slice(0, -1));

console.log(array);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

Use split to remove the ;

var a = ['0ad39f02478f5;',
  '309c9e9051482;',
  'dd7d5faf07569;',
  'ghdg9f02478f5;',
  '30967uj051482;',
  'ad398ixc478f5;',
  '39c9frtuyr482;'
];
if(a.length>0)
a[a.length - 1] = a[a.length - 1].split(';')[0]
console.log(a)

using replace

var a = ['0ad39f02478f5;',
  '309c9e9051482;',
  'dd7d5faf07569;',
  'ghdg9f02478f5;',
  '30967uj051482;',
  'ad398ixc478f5;',
  '39c9frtuyr482;'
];
if(a.length>0)
a[a.length - 1] = a[a.length - 1].replace(';','');
console.log(a)
ellipsis
  • 12,049
  • 2
  • 17
  • 33
2

Your implementation is almost spot on. You just have to consider value[arrayLength].replace(';', '') does not actually change the value but returns a new value back. So assign the new value as such

...
value[arrayLength] = value[arrayLength].replace(';', '')
...

Also you don't need to iterate through all the items as we just want to grab the last one. So here is your solution tweaked.

const arrayOfJobs = [
  '0ad39f02478f5;',
  '309c9e9051482;',
  'dd7d5faf07569;',
  'ghdg9f02478f5;',
  '30967uj051482;',
  'ad398ixc478f5;',
  '39c9frtuyr482;'
];

arrayOfJobs[arrayOfJobs.length - 1] = arrayOfJobs[arrayOfJobs.length - 1].replace(';', '');

console.log(arrayOfJobs);
1

You can use map to get a copy of the initial array with the last value altered:

const data = [ 
  '0ad39f02478f5;',
  '309c9e9051482;',
  'dd7d5faf07569;',
  'ghdg9f02478f5;',
  '30967uj051482;',
  'ad398ixc478f5;',
  '39c9frtuyr482;' 
  ];
  
function getFinalArray(arr) {
  return arr.map((x, i) => {
    if (i === arr.length - 1)
      return x.replace(';', '');
    return x;
  });
}

console.log(getFinalArray(data));
kapantzak
  • 11,610
  • 4
  • 39
  • 61
0

in a simple way,

l[lastIndex] = l[lastIndex].replace('from', 'to')

to get last index => [].length-1

l = ['0ad39f02478f5;', '309c9e9051482;', 'dd7d5faf07569;', 'ghdg9f02478f5;', '30967uj051482;', 'ad398ixc478f5;', '39c9frtuyr482;']
l[l.length-1] = l[l.length-1].replace(';', '');
console.log('my list', l);
Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
0

A simple solution

var x = [ '0ad39f02478f5;',
  '309c9e9051482;',
  'dd7d5faf07569;',
  'ghdg9f02478f5;',
  '30967uj051482;',
  'ad398ixc478f5;',
  '39c9frtuyr482;' ]

var len = x.length-1
x[len] = x[len].replace(";", "")
console.log(x)
Dupinder Singh
  • 7,175
  • 6
  • 37
  • 61