将对象转换为返回同一对象的函数

时间:2017-07-10 07:28:17

标签: javascript function object

我知道这个问题看似简单,但我在这里遇到了困难。我有一个对象,键有一些国家的代码,值是每个代码的翻译消息。

我的目标就在这里:

import React from 'react';
import { FormattedMessage } from 'react-intl';
/*eslint-disable */
const countries = {
  'AU' : {
    name: <FormattedMessage
      id="australia"
      defaultMessage="_australia_"
      description="the message for australia"
    />,
  },
  'AT' : {
    name: <FormattedMessage
      id="austria"
      defaultMessage="_austria_"
      description="the message for austria"
    />,
  },
  'BE' : {
    name: <FormattedMessage
      id="belgium"
      defaultMessage="_belgium_"
      description="the message for belgium"
    />,
  },
  'BG' : {
    name: <FormattedMessage
      id="bulgary"
      defaultMessage="bulgary"
      description="the message for bulgary"
    />,
  },
  'CA' : {
    name: <FormattedMessage
      id="canada"
      defaultMessage="_canada_"
      description="the message for canada"
    />,
  },
  'FR' : {
    name: <FormattedMessage
      id="france"
      defaultMessage="_france_"
      description="the message for france"
    />,
  },
  'DE' : {
    name: <FormattedMessage
      id="germany"
      defaultMessage="_germany_"
      description="the message for germany"
    />,
  },
  'GR' : {
    name: <FormattedMessage
      id="greece"
      defaultMessage="_greece_"
      description="the message for greece"
    />,
  },
  'HK' : {
    name: <FormattedMessage
      id="hongKong"
      defaultMessage="_hongKong_"
      description="the message for hongKong"
    />,
  },
  'HU' : {
    name: <FormattedMessage
      id="hungary"
      defaultMessage="_hungary_"
      description="the message for hungary"
    />,
  },
  'IE' : {
    name: <FormattedMessage
      id="ireland"
      defaultMessage="_ireland_"
      description="the message for ireland"
    />,
  },
  'IT' : {
    name: <FormattedMessage
      id="italia"
      defaultMessage="_italia_"
      description="the message for italia"
    />,
  },
  'LR' : {
    name: <FormattedMessage
      id="liberia"
      defaultMessage="_liberia_"
      description="the message for liberia"
    />,
  },
  'LU' : {
    name: <FormattedMessage
      id="luxemburg"
      defaultMessage="_luxemburg_"
      description="the message for luxemburg"
    />,
  },
  'NL' : {
    name: <FormattedMessage
      id="holland"
      defaultMessage="_holland_"
      description="the message for holland"
    />,
  },
  'NO' : {
    name: <FormattedMessage
      id="norway"
      defaultMessage="_norway_"
      description="the message for norway"
    />,
  },
  'PL' : {
    name: <FormattedMessage
      id="poland"
      defaultMessage="_poland_"
      description="the message for poland"
    />,
  },
  'PT' : {
    name: <FormattedMessage
      id="portogal"
      defaultMessage="_portogal_"
      description="the message for portogal"
    />,
  },
  'RS' : {
    name: <FormattedMessage
      id="serbia"
      defaultMessage="_serbia_"
      description="the message for serbia"
    />,
  },
  'ES' : {
    name: <FormattedMessage
      id="spain"
      defaultMessage="spain"
      description="the message for spain"
    />,
  },
  'SE' : {
    name: <FormattedMessage
      id="sweden"
      defaultMessage="_sweden_"
      description="the message for sweden"
    />,
  },
  'SH' : {
    name: <FormattedMessage
      id="switzerland"
      defaultMessage="_switzerland_"
      description="the message for switzerland"
    />,
  },
  'GB' : {
    name: <FormattedMessage
      id="unitedKingdom"
      defaultMessage="_unitedKingdom_"
      description="the message for unitedKingdom"
    />,
  },
  'US' : {
    name: <FormattedMessage
      id="usa"
      defaultMessage="_usa_"
      description="the message for usa"
    />,
  },
  'SG' : {
    name: <FormattedMessage
      id="singapore"
      defaultMessage="_singapore_"
      description="the message for singapore"
    />
  },
  'L1' : {
    name: <FormattedMessage
      id="londonInt"
      defaultMessage="_londonInt_"
      description="the message for londonInt"
    />
  },
  'DK' : {
    name: <FormattedMessage
      id="denmark"
      defaultMessage="_denmark_"
      description="the message for denmark"
    />,
  },
  'FI' : {
    name: <FormattedMessage
      id="finland"
      defaultMessage="_finland_"
      description="the message for finland"
    />,
  },
  'JP' : {
    name: <FormattedMessage
      id="japan"
      defaultMessage="_japan_"
      description="the message for japan"
    />,
  },
  'CY' : {
    name: <FormattedMessage
      id="cyprus"
      defaultMessage="_cyprus_"
      description="the message for cyprus"
    />,
  },
};

export default countries;

我想将它添加到一个返回此对象的函数中,所以我写了这个:

const countriesRenderer = () => {
return (
'AU' : {
    name: <FormattedMessage
      id="australia"
      defaultMessage="_australia_"
      description="the message for australia"
    />,
  },
  'AT' : {
    name: <FormattedMessage
      id="austria"
      defaultMessage="_austria_"
      description="the message for austria"
    />,
  },
  'BE' : {
    name: <FormattedMessage
      id="belgium"
      defaultMessage="_belgium_"
      description="the message for belgium"
    />,
  },
  'BG' : {
    name: <FormattedMessage
      id="bulgary"
      defaultMessage="bulgary"
      description="the message for bulgary"
    />,
  },
  'CA' : {
    name: <FormattedMessage
      id="canada"
      defaultMessage="_canada_"
      description="the message for canada"
    />,
  },
  'FR' : {
    name: <FormattedMessage
      id="france"
      defaultMessage="_france_"
      description="the message for france"
    />,
  },
  'DE' : {
    name: <FormattedMessage
      id="germany"
      defaultMessage="_germany_"
      description="the message for germany"
    />,
  },
  'GR' : {
    name: <FormattedMessage
      id="greece"
      defaultMessage="_greece_"
      description="the message for greece"
    />,
  },
  'HK' : {
    name: <FormattedMessage
      id="hongKong"
      defaultMessage="_hongKong_"
      description="the message for hongKong"
    />,
  },
  'HU' : {
    name: <FormattedMessage
      id="hungary"
      defaultMessage="_hungary_"
      description="the message for hungary"
    />,
  },
  'IE' : {
    name: <FormattedMessage
      id="ireland"
      defaultMessage="_ireland_"
      description="the message for ireland"
    />,
  },
  'IT' : {
    name: <FormattedMessage
      id="italia"
      defaultMessage="_italia_"
      description="the message for italia"
    />,
  },
  'LR' : {
    name: <FormattedMessage
      id="liberia"
      defaultMessage="_liberia_"
      description="the message for liberia"
    />,
  },
  'LU' : {
    name: <FormattedMessage
      id="luxemburg"
      defaultMessage="_luxemburg_"
      description="the message for luxemburg"
    />,
  },
  'NL' : {
    name: <FormattedMessage
      id="holland"
      defaultMessage="_holland_"
      description="the message for holland"
    />,
  },
  'NO' : {
    name: <FormattedMessage
      id="norway"
      defaultMessage="_norway_"
      description="the message for norway"
    />,
  },
  'PL' : {
    name: <FormattedMessage
      id="poland"
      defaultMessage="_poland_"
      description="the message for poland"
    />,
  },
  'PT' : {
    name: <FormattedMessage
      id="portogal"
      defaultMessage="_portogal_"
      description="the message for portogal"
    />,
  },
  'RS' : {
    name: <FormattedMessage
      id="serbia"
      defaultMessage="_serbia_"
      description="the message for serbia"
    />,
  },
  'ES' : {
    name: <FormattedMessage
      id="spain"
      defaultMessage="spain"
      description="the message for spain"
    />,
  },
  'SE' : {
    name: <FormattedMessage
      id="sweden"
      defaultMessage="_sweden_"
      description="the message for sweden"
    />,
  },
  'SH' : {
    name: <FormattedMessage
      id="switzerland"
      defaultMessage="_switzerland_"
      description="the message for switzerland"
    />,
  },
  'GB' : {
    name: <FormattedMessage
      id="unitedKingdom"
      defaultMessage="_unitedKingdom_"
      description="the message for unitedKingdom"
    />,
  },
  'US' : {
    name: <FormattedMessage
      id="usa"
      defaultMessage="_usa_"
      description="the message for usa"
    />,
  },
  'SG' : {
    name: <FormattedMessage
      id="singapore"
      defaultMessage="_singapore_"
      description="the message for singapore"
    />
  },
  'L1' : {
    name: <FormattedMessage
      id="londonInt"
      defaultMessage="_londonInt_"
      description="the message for londonInt"
    />
  },
  'DK' : {
    name: <FormattedMessage
      id="denmark"
      defaultMessage="_denmark_"
      description="the message for denmark"
    />,
  },
  'FI' : {
    name: <FormattedMessage
      id="finland"
      defaultMessage="_finland_"
      description="the message for finland"
    />,
  },
  'JP' : {
    name: <FormattedMessage
      id="japan"
      defaultMessage="_japan_"
      description="the message for japan"
    />,
  },
  'CY' : {
    name: <FormattedMessage
      id="cyprus"
      defaultMessage="_cyprus_"
      description="the message for cyprus"
    />,
  },
);
}

但它似乎不起作用......任何想法我缺少什么?

1 个答案:

答案 0 :(得分:1)

您应该尝试使用{ ... }来包装您的对象,它将解决您的问题。

修改后的代码:

const countriesRenderer = () => {
  return ({
    'AU': {
      name: < FormattedMessage
      id = "australia"
      defaultMessage = "_australia_"
      description = "the message for australia" /
      > ,
    },
    'AT': {
      name: < FormattedMessage
      id = "austria"
      defaultMessage = "_austria_"
      description = "the message for austria" /
      > ,
    },
    'BE': {
      name: < FormattedMessage
      id = "belgium"
      defaultMessage = "_belgium_"
      description = "the message for belgium" /
      > ,
    },
    'BG': {
      name: < FormattedMessage
      id = "bulgary"
      defaultMessage = "bulgary"
      description = "the message for bulgary" /
      > ,
    },
    'CA': {
      name: < FormattedMessage
      id = "canada"
      defaultMessage = "_canada_"
      description = "the message for canada" /
      > ,
    },
    'FR': {
      name: < FormattedMessage
      id = "france"
      defaultMessage = "_france_"
      description = "the message for france" /
      > ,
    },
    'DE': {
      name: < FormattedMessage
      id = "germany"
      defaultMessage = "_germany_"
      description = "the message for germany" /
      > ,
    },
    'GR': {
      name: < FormattedMessage
      id = "greece"
      defaultMessage = "_greece_"
      description = "the message for greece" /
      > ,
    },
    'HK': {
      name: < FormattedMessage
      id = "hongKong"
      defaultMessage = "_hongKong_"
      description = "the message for hongKong" /
      > ,
    },
    'HU': {
      name: < FormattedMessage
      id = "hungary"
      defaultMessage = "_hungary_"
      description = "the message for hungary" /
      > ,
    },
    'IE': {
      name: < FormattedMessage
      id = "ireland"
      defaultMessage = "_ireland_"
      description = "the message for ireland" /
      > ,
    },
    'IT': {
      name: < FormattedMessage
      id = "italia"
      defaultMessage = "_italia_"
      description = "the message for italia" /
      > ,
    },
    'LR': {
      name: < FormattedMessage
      id = "liberia"
      defaultMessage = "_liberia_"
      description = "the message for liberia" /
      > ,
    },
    'LU': {
      name: < FormattedMessage
      id = "luxemburg"
      defaultMessage = "_luxemburg_"
      description = "the message for luxemburg" /
      > ,
    },
    'NL': {
      name: < FormattedMessage
      id = "holland"
      defaultMessage = "_holland_"
      description = "the message for holland" /
      > ,
    },
    'NO': {
      name: < FormattedMessage
      id = "norway"
      defaultMessage = "_norway_"
      description = "the message for norway" /
      > ,
    },
    'PL': {
      name: < FormattedMessage
      id = "poland"
      defaultMessage = "_poland_"
      description = "the message for poland" /
      > ,
    },
    'PT': {
      name: < FormattedMessage
      id = "portogal"
      defaultMessage = "_portogal_"
      description = "the message for portogal" /
      > ,
    },
    'RS': {
      name: < FormattedMessage
      id = "serbia"
      defaultMessage = "_serbia_"
      description = "the message for serbia" /
      > ,
    },
    'ES': {
      name: < FormattedMessage
      id = "spain"
      defaultMessage = "spain"
      description = "the message for spain" /
      > ,
    },
    'SE': {
      name: < FormattedMessage
      id = "sweden"
      defaultMessage = "_sweden_"
      description = "the message for sweden" /
      > ,
    },
    'SH': {
      name: < FormattedMessage
      id = "switzerland"
      defaultMessage = "_switzerland_"
      description = "the message for switzerland" /
      > ,
    },
    'GB': {
      name: < FormattedMessage
      id = "unitedKingdom"
      defaultMessage = "_unitedKingdom_"
      description = "the message for unitedKingdom" /
      > ,
    },
    'US': {
      name: < FormattedMessage
      id = "usa"
      defaultMessage = "_usa_"
      description = "the message for usa" /
      > ,
    },
    'SG': {
      name: < FormattedMessage
      id = "singapore"
      defaultMessage = "_singapore_"
      description = "the message for singapore" /
      >
    },
    'L1': {
      name: < FormattedMessage
      id = "londonInt"
      defaultMessage = "_londonInt_"
      description = "the message for londonInt" /
      >
    },
    'DK': {
      name: < FormattedMessage
      id = "denmark"
      defaultMessage = "_denmark_"
      description = "the message for denmark" /
      > ,
    },
    'FI': {
      name: < FormattedMessage
      id = "finland"
      defaultMessage = "_finland_"
      description = "the message for finland" /
      > ,
    },
    'JP': {
      name: < FormattedMessage
      id = "japan"
      defaultMessage = "_japan_"
      description = "the message for japan" /
      > ,
    },
    'CY': {
      name: < FormattedMessage
      id = "cyprus"
      defaultMessage = "_cyprus_"
      description = "the message for cyprus" /
      > ,
    }
  });
}