user1076698 user1076698 - 1 year ago 58
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 Source

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 =, o => _.mapObject(o, value => value.toString()))

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

var result =, o => _.mapObject(o, String))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download