0

i have an array output

["AA janral store shahid bhai Islam pura,31.592887,74.4292156,1~", "Almakkah store shahid bhai gunj bazar,31.5645088,74.3838828,1~", "hamad hassan Nafees backery,31.595234,74.3565654,1~"]

but required output is in javascript  

[Array(), Array(), Array()]

example

var ar = [
      ['AA janral store shahid bhai Islam pura',31.592887,74.4292156,1],//(array)
      ['Almakkah store shahid bhai gunj bazar',31.5645088,74.3838828,1],//(array)
      ['hamad hassan Nafees backery',31.595234,74.3565654,1]//(array)
     ];

1 Answers1

1

If commas are always separators, so:

const arr = ["AA janral store shahid bhai Islam pura,31.592887,74.4292156,1~", "Almakkah store shahid bhai gunj bazar,31.5645088,74.3838828,1~", "hamad hassan Nafees backery,31.595234,74.3565654,1~"];
const res = [...arr.map(item => item.split(","))];
console.log(res);

The same with filtering blank strings:

const arr = ["AA janral store shahid bhai Islam pura,31.592887,74.4292156,1~", "", "Almakkah store shahid bhai gunj bazar,31.5645088,74.3838828,1~", "hamad hassan Nafees backery,31.595234,74.3565654,1~"];
const res = [...arr.reduce((items, item) => (item.length && (items = [...items, item.split(",")]), items), [])];
console.log(res);

With different variants of incoming data:

const arr = ["AA janral store shahid bhai Islam pura,31.592887,74.4292156, ", "", " ", ",,,,,", "Almakkah store shahid bhai gunj bazar,31.5645088,74.3838828,1~", "hamad hassan Nafees backery,31.595234,74.3565654,1~"];
const res = [...arr.reduce((items, item) => {
        const splitted = item.split(",").filter(item => item.trim().length);
        if (splitted.length) items.push(splitted);
        return items;
    }, [])];
console.log(res);
renich
  • 146
  • 1
  • 5
  • you r genious:) thanx helped –  Oct 22 '20 at 09:08
  • but help me in one thing in the last it passes " "(that) i dont want (" ") example ["AA janral store shahid bhai Islam pura,31.592887,74.4292156,1", " "] –  Oct 22 '20 at 09:13
  • I added an example with an empty string. – renich Oct 22 '20 at 09:56
  • I also added the most complex example that I could think of. – renich Oct 22 '20 at 10:41
  • legend bro but i want some more complexity like ["AA janral store shahid bhai Islam pura", 31.592887, 74.4292156, 1] means [ "string" , 1234,456 , 1] i am spliting value using '~' but in return it pass me string ["Almakkah store shahid bhai gunj bazar", "31.5645088,74.3838828,1", ""] but required is [ "string in cotation" , number without double cotation , 1] –  Oct 22 '20 at 11:19