浙江凌宇环境建设公司网站,专做宝宝辅食的网站,广州海珠区房价,天津河东做网站贵吗要想将对象转换为格式化 JSON 展示在文本框中#xff0c;需要用到 JSON.stringify
JSON.stringify 方法接受三个参数#xff1a;
value#xff1a;必需#xff0c;一个 JavaScript 值#xff08;通常为对象或数组#xff09;要转换为 JSON 字符串。replacer#xff1a…要想将对象转换为格式化 JSON 展示在文本框中需要用到 JSON.stringify
JSON.stringify 方法接受三个参数
value必需一个 JavaScript 值通常为对象或数组要转换为 JSON 字符串。replacer可选一个函数或数组用于在序列化过程中过滤或转换值。space可选用于在输出 JSON 字符串中添加缩进、空格或换行符以提高可读性。如果是一个数字则它表示每个缩进级别的空格数如果是一个字符串则该字符串用作每个缩进级别的缩进。
具体代码
import { Button, Input } from antd;export default () {const value {name: 张三,age: 20,gender: 男,address: {city: 北京,street: 朝阳路,zipCode: 100000},phoneNumbers: [13800000001, 13900000002],isStudent: true,};const replacerFunc (key, value) {if (key phoneNumbers) {return value.map(item {// 将手机号中间四位替换为占位符return item.slice(0, 3) **** item.slice(7); })}return value;}const defaultValue JSON.stringify(value, replacerFunc, 4)return (Input.TextArea defaultValue{defaultValue} /)
};展示效果