引用对象数组的所有值

时间:2016-11-07 11:46:54

标签: javascript underscore.js

我有一个对象数组

"

如何将引号var obj = [{ key1: "1", key2: "2" }, { key1: "1", key2: "2", key3: "hello" }]; 附加到对象数组中的所有值。

结果:

<VirtualHost *:80>
                ServerName example.com
                ServerAdmin me@example.com
                WSGIScriptAlias /var/www/vhost/{domain}/{subdomain}/flaskapp.wsgi
                <Directory /var/www/vhost/{domain}/{subdomain}/static>
                        Order allow,deny
                        Allow from all
                </Directory>
                Alias /static /var/www/vhost/{domain}/{subdomain}/static
                <Directory /var/www/vhost/{domain}/{subdomain}/static>
                        Order allow,deny
                        Allow from all
                </Directory>
                ErrorLog ${APACHE_LOG_DIR}/error.log
                LogLevel warn
                CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

1 个答案:

答案 0 :(得分:2)

您可以在数组上map,然后在数组中的每个对象上使用mapObject,使用toString将数字转换为字符串:

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

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

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