-2

Here is my array:

const main = [
  [['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369']],
];

and here is my function:

const convertor = (x) => {
  const splitted = x.split(':');
  console.log(splitted);
  const converted = splitted[0] * 60 + splitted[1] * 60 + splitted[2];
  return converted;
};

I want to map this function on each nested array

I tried this but I got an error:

const resu = main.map((x) => {
  x.map((y) => {
    convertor(y);
  });
});

x.split is not a function

8
  • 3
    what is the expected output Commented Nov 29, 2021 at 12:24
  • Does this answer your question? Map an array of arrays Commented Nov 29, 2021 at 12:25
  • @KrzysztofKrzeszewski expected output should be timestamps converted to seconds. for example : 02:50:20,656 turns into : 312020,656 Commented Nov 29, 2021 at 12:28
  • @moemous Got the answer 132021,369, please check my code? Commented Nov 29, 2021 at 12:29
  • The .map() callback has to return the new value. Your callbacks don't return anything. Commented Nov 29, 2021 at 12:29

4 Answers 4

1

You made some mistakes:

  1. Expression (x) => { /* a few lines of code */ } requires the use of the return keyword to return the result, while (x) => /* single line of code */ doesn't.

  2. Your array main is three-dimensional array, not two-dimensional.

Try this:

const resu = main.map((x) => {
  return x.map((y) => {
    return y.map(convertor);
  });
});

Or easier:

const resu = main.map(
    (x) => x.map(
        (y) => y.map(convertor)
    )
);

const main = [
  [['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369']],
];

const convertor = (x) => {
  const splitted = x.split(':');
  const converted = splitted[0] * 60 + splitted[1] * 60 + splitted[2];
  return converted;
};

const resu = main.map(
    (x) => x.map(
        (y) => y.map(convertor)
    )
);

console.log(resu);

Sign up to request clarification or add additional context in comments.

1 Comment

"while (x) => /* single line of code */ doesn't" - That's not a matter of single line or not. { } creates a block. And that's what triggers the need of an explicit return. This is also the reason why you have to wrap an object in ( ) without an explicit return: (foo) => ({ bar: foo })
1

Issues

  1. Note that there are 3 level nested arrays, so there should be 3 maps()
  2. return is required if you use {} in the arrow function

const main = [
  [['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369']],
];

const convertor = (x) => {
  const splitted = x.split(':');
  const converted = splitted[0] * 60 + splitted[1] * 60 + splitted[2];
  return converted;
};

const resu = main.map((x) => {
  return x.map((y) => {
    return y.map((z) => {
      return convertor(z);
    });
  });
});

console.log(resu);

Shorter version

main.map(x => x.map(y => y.map(convertor)));

Comments

1

Another approach could be to recursively go through the array(s). This way you are not limited to a specific depth level.

const main = [
  [['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369']],
];

const convertor = (x) => {
  const splitted = x.split(':');
  // console.log(splitted);
  const converted = splitted[0] * 60 + splitted[1] * 60 + splitted[2];
  return converted;
};

const convertTimeToSec = (arr) => {
  for(let i = 0, length = arr.length; i < length; i++) {
    if(Array.isArray(arr[i])) {
      arr[i] = convertTimeToSec(arr[i]);
    }else{
      arr[i] = convertor(arr[i]);
    }
  }
  
  return arr;
}

console.log(convertTimeToSec(main));

Comments

1

const main = [
  [
    ['02:20:21,369'],
    ['02:20:21,369'],
    ['02:20:21,369'],
    ['02:20:21,369']
  ],
  [
    ['02:20:21,369'],
    ['02:20:21,369']
  ],
  [
    ['02:20:21,369'],
    ['02:20:21,369'],
    ['02:20:21,369']
  ],
  [
    ['02:20:21,369']
  ],
];


const convertor = (x) => {
  const splitted = x.split(':');
  //console.log(splitted);
  const converted = splitted[0] * 60 + splitted[1] * 60 + splitted[2];
  return converted;
};

const mappedMain = main.map(i => {
 return i.map(j => {
  return convertor(...j)
 })
})

//Or
//const mappedMain = main.map(i => i.map(j => convertor(...j)))



console.log(mappedMain);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.