Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wingmeng opened this issue May 30, 2019 · 0 comments
Open

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

wingmeng opened this issue May 30, 2019 · 0 comments

Comments

@wingmeng
Copy link
Collaborator

wingmeng commented May 30, 2019

题目:

已知如下数据:

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, '');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant