Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

count the number of characters in 1 format

I have a text whose type is string and not array, as shown below:

'[123,456],[789],[10]'
  1. I want the count of number of arrays in this string(in this case it will be 3)
  2. I want the number of elements in each array.

I can’t use split with commas because there are commas between the numbers in that "array".

And I need to determine the length of those 3 arrays, how do I do that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Desired result:

array: 3
length: 2,1,1

>Solution :

You could add an array in the string so you end up with an actual json array.

Then you can parse the json and count them.

let value = '[' + '[123,456],[789],[10]' + ']';
let json = JSON.parse(value);
json.forEach((item, i) => {
  console.log('length: ' + item.length)
});
console.log(json.length + ' items')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading