Visitar URL original
第 21 期(JSON):JSON.stringify · Issue #24 · fezaoduke/fe-practice-hard · GitHub
Skip to content

第 21 期(JSON):JSON.stringify #24

Description

@wingmeng

题目:

已知如下数据:

let country = {
  name: 'China',
  language: 'Chinese',
  population: {
    value: 14,
    unit: '亿'
  }
};

请使用 JSON.stringify 将其转化为以下格式的字符串:

--"name": "China",
--"language": "Chinese",
--"population": "14 亿"

参考答案:

JSON.stringify(country, (k, v) => {
  if (k === 'population') {
    return `${v.value} ${v.unit}`;
  }

  return v;
}, '--').replace(/\{|\}/g, '');

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions