var myObj;
myObj = [{ "name":"John", "age":30, "car":"maruti" },{ "name":"Smith", "age":50, "car":"indica" }];
我想得到json
[{ "name":"", "age":, "car":"" },{ "name":"", "age":, "car":"" }];
请解决此问题
答案 0 :(得分:1)
只需使用map
和for
var myObj = [{ "name":"John", "age":30, "car":"maruti" },{ "name":"Smith", "age":50, "car":"indica" }];
var result = myObj.map(v=>{
v = Object.assign({}, v);
for( let k in v ) v[k] = "";
return v;
});
console.log( result );
仅改变年龄。
var myObj = [{ "name":"John", "age":30, "car":"maruti" },{ "name":"Smith", "age":50, "car":"indica" }];
var result = myObj.map(v=>{
v = Object.assign({}, v);
v.age = "";
return v;
});
console.log( result );
答案 1 :(得分:0)
这是一个不会改变原始值的解决方案:
myObj.map(x => Object.keys(x).reduce((total, value) => { total[value] = ''; return total; }, {}));