user1076698 user1076698 - 1 month ago 6
Javascript Question

Quotes all values of an array of objects

I have an array of objects

var obj = [{ key1: 1, key2: 2 }, { key1: 1, key2: 2 }];


How can I append quotes
"
to all values in the array of objects.

Result :

var obj = [{ key1: "1", key2: "2" }, { key1: "1", key2: "2", key3: "hello" }];

Answer

You could map over the array and then use mapObject on each object in the array to convert the number to a string using toString:

var result = _.map(obj, o => _.mapObject(o, value => value.toString()))

Or as torazaburo mentioned in the comment, use the String constructor as the iteratee:

var result = _.map(obj, o => _.mapObject(o, String))