javascript - Iterate over nested objects and concatenate to string using Lodash -
javascript - Iterate over nested objects and concatenate to string using Lodash -
i’m using lodash create manipulating objects easier. have object 3 nested objects inside. i’d iterate through these, concatenating of respective children in possible combinations, whilst using 1 per list.
my object looks this:
{ "list_1": { "1": ".cat-3", "2": ".cat-5", "3": ".cat-7" }, "list_2": { "1": ".eyes-blue", "3": ".eyes-brown" }, "list_3": { "1": ".jazz", "2": ".commercial", "3": ".hip-hop" } }
the output i’d is:
.cat-3.eyes-blue.jazz .cat-3.eyes-blue.commercial .cat-3.eyes-blue.hip-hop
the order isn't crucial. what's crucial 1 value each list_
object used in string. this, example, fine:
.eyes-blue.jazz.cat-3 .eyes-blue.cat-3.commercial .hip-hop.eyes-blue.cat-3
and more examples:
.cat-3.eyes-brown.jazz .cat-5.eyes-brown.hip-hop .cat-7.eyes-blue.hip-hop
store values of property objects array of arrays:
var arrayofarrays = []; _.each(obj, function(item, key) { var itemvals = []; _.each(item, function(item2, key2) { itemvals.push(item2); }); arrayofarrays.push(itemvals); });
implement suffle()
function described here: http://stackoverflow.com/a/2450976/2193416
implement function random array element:
function randomelement(array) { homecoming array[math.floor(math.random() * array.length)]; }
implement function select random elements array of arrays concatenated string:
function randomcombination(arrayofarrays) { var output = ""; _.each(arrayofarrays, function(innerarray) { output += randomelement(innerarray); }) homecoming output }
now can desired output doing like:
randomcombination(shuffle(arrayofarrays));
or, if want maintain arrayofarrays intact:
randomcombination(shuffle(arrayofarrays.slice(0)));
javascript arrays object javascript-objects lodash
Comments
Post a Comment