JS 替换 JSON 数组中的指定字段名

直接链式操作

JSON.parse(JSON.stringify(你的json数组).replace(/原来的字段名/g, "想要的字段名"));

例如:

const list = [{ id: "1", area: "南明区" }, { id: "2", area: "云岩区" }];

console.log(JSON.parse(JSON.stringify(list).replace(/area/g, "text"))); // [{ id: "1", text: "南明区" }, { id: "2", text: "云岩区" }]

猜你喜欢

转载自blog.csdn.net/AdminGuan/article/details/132734424