How can I map a nested array in JS

51
November 29, 2021, at 10:00 PM

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

Answer 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);

Answer 2

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)));
Answer 3

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));

Answer 4

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);

READ ALSO
Android 11: How to access app specific files from Browser

Android 11: How to access app specific files from Browser

My app is creating some html files (reports) that user can open by using web browser

72
Intellij IDEA, java, maven, can&#180;t use imported library [closed]

Intellij IDEA, java, maven, can´t use imported library [closed]

Want to improve this question? Add details and clarify the problem by editing this post

63
shap.DeepExplainer gives an error related to GlobalMaxPooling1D layer of CNN

shap.DeepExplainer gives an error related to GlobalMaxPooling1D layer of CNN

I am getting an error as below whenever I want to explain CNN classifier with SHAP for sentiment analysis:

55
How can I make a constraint so that the appointment cannot be repeated?

How can I make a constraint so that the appointment cannot be repeated?

I want to make a constraint so that the appointment with a certain doctor cannot be repeated more than onceFor example, if a person chooses an appointment on 11/09/2021 from 15:00 until 15:30, the same appointment cannot be booked again at the same doctor

79