arrays - How to Format JavaScript String based on value -
i have string variable object value in format:
[" traffic landing hits: 0, rewards hits: 0 engagement facebook posts: 0, twitter tweets: 0, twitter autofollows: 0, instagram photos: 0, instagram likes: 0, instagram votes: 0, pinterest pins: 0, form submissions: 0 conversion submissions: 0, engagement: 0, views: 0, prints: 0 "]
i want convert into:
["traffic", "landing hits: 0", "rewards hits: 0"], ["engagement", "facebook likes: 0", "facebook posts: 0", "facebook share: 0", "form submissions: 0", "instagram likes: 0", "instagram photos: 0", "instagram votes: 0", "pinterest pins: 0", "twitter autofollows: 0", "twitter tweets: 0"], ["conversion", "conversion rate: 0", "offers emailed: 0", "prints: 0", "submissions: 0", "views: 0"]
any ideas how this?
you don't need advanced regex split array of arrays. here's simple (but perhaps not best) solution:
var before = "\t\t\ttraffic\n\t\tlanding hits: 0, rewards hits: 0\n\t\t\tengagement\n\t\tfacebook posts: 0, twitter tweets: 0, twitter autofollows: 0, instagram photos: 0, instagram likes: 0, instagram votes: 0, pinterest pins: 0, form submissions: 0\n\t\t\tconversion\n\t\tsubmissions: 0, engagement: 0, views: 0, prints: 0"; var pieces = before.replace(/\t/g, '').split('\n'); var group; var result = []; (var = 0; < pieces.length; i++) { if (i % 2) { result.push(group.concat(pieces[i].split(/\s*,\s*/))); } else { group = [pieces[i]]; } } console.log(result);
Comments
Post a Comment