fezaoduke / fe-practice-hard

晚练课

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

第 21 期(JSON):JSON.stringify

wingmeng opened this issue · comments

题目:

已知如下数据:

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, '');