谈到数组与Underscore.js对象的数组? [英] Turning an array into an array of objects with Underscore.js?

查看:108
本文介绍了谈到数组与Underscore.js对象的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组:

  VAR国家= ['奥','美国','澳'];

我知道你可以把它转换成一个对象,像这样Underscore.js:

  _对象(['名','名2','NAME3'],国家));

我怎么可以把数组对象的数组,看起来像这样?

  VAR countriesObject = [
    {名称:'奥'},
    {名称:'美'},
    {名称:'澳大利亚'}
  ];

(所有的键名为名称)。


解决方案

  VAR countriesObject = _.map(国家,函数(国家){
    返回{
            名称:国
     }
 }

I have an array:

var countries = ['Austria', 'America', 'Australia'];

I know you can turn that into an object with Underscore.js like this:

_.object(['name', 'name2', 'name3'], countries));

How can I turn the array into an array of objects that looks like this?

  var countriesObject = [
    { name: 'Austria' },
    { name: 'America' },
    { name: 'Australia' }
  ];

(with all the keys named name).

解决方案

var countriesObject = _.map (countries,function (country){
    return {
            name: country
     }
 }

这篇关于谈到数组与Underscore.js对象的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆