引用对象数组的所有值

Quotes all values of an array of objects

我有一个对象数组

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

如何将引号 " 附加到对象数组中的所有值。

结果:

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

你可以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()))

或者如评论中提到的torazaburo,使用String构造函数作为iteratee:

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