underscore.js - Underscore _.flatten with prefix of the to be flatten object -
i need flatten object this:
{ productid: { id: "123456-1234-1234-1234-123456789012", type: "product", name: "product" } }
to like
{ productid_id: "123456-1234-1234-1234-123456789012", productid_type: "product", productid_name: "product" }
and i'm trying archive underscore, _.flatten doesn't have have parameter that.
you can .map , .reduce
var raw = { productid: { id: "123456-1234-1234-1234-123456789012", type: "product", name: "product" } }, flatten = _.map(raw, function(item, k){ return _.reduce(item, function(obj, val, key){ obj[[k,key].join('_')] = val; return obj; }, {}); }); console.log(flatten[0])
Comments
Post a Comment