Create specific format string from array - jquery -
its simple question
i have 1 jquery arrary
arry = [ 'apple=1', 'apple=2', 'orange=5', 'orange=7', 'orange=9', 'guava=12', 'guava=11' ]; i want convert above array string
str = 'apple=1*2*~orange=5*7*9~guava=12*11'; kindly me in this...
(actually looking various interesting ways this)
there more straightforward ways, if know you're going exact output, find useful first rearrange array in proper dictionary:
var dict = {}; arry.foreach(function(item) { var parts = item.split('='); var key = parts[0]; var value = parts[1]; if(key in dict) { dict[key].push(value); } else { dict[key] = [value]; } }); ... , concatenate string that:
object.keys(dict).map(function(key) { return [key, '=', dict[key].join('*')].join(''); }).join('~');
Comments
Post a Comment