0

I have this array:

let arr = ['0 abc', '81 abc', '63 abc', '108 abc', '100 abc', '80 abc', '54 abc', '128 abc', '32 abc', '9 abc'];

I want this final result:

{
 '0': '0 abc',
 '81': '81 abc',
 '63': '63 abc',
 '108': '108 abc',
 ...
}

So I do this function:

function arrayObj(arr){
    let obj = arr.reduce((prevVal, item) => {
        let sp = item.split(' ');
        let number = sp.splice(0,1);
    prevVal[number] = item;
    return prevVal;
}, {});

return obj;

But my function log this in other sequence of the original array:

{
      '0': '0 abc',
      '9': '9 abc',
      '32': '32 abc',
      '54': '54 abc',
      '63': '63 abc',
      '80': '80 abc',
      '81': '81 abc',
      '100': '100 abc',
      '108': '108 abc',
      '128': '128 abc'
    }

What I do wrong?

eduardo
  • 11
  • 4

0 Answers0