0

var myObj;

myObj = [{ "name":"John", "age":30, "car":"maruti" },{ "name":"Smith", "age":50, "car":"indica" }];

I want to get json

[{ "name":"", "age":, "car":"" },{ "name":"", "age":, "car":"" }];

please solve this problem

Poorna Senani Gamage
  • 1,246
  • 2
  • 19
  • 30

2 Answers2

1

Just use map and 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 );

Changing the age only.

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 );
Eddie
  • 26,593
  • 6
  • 36
  • 58
  • ok fine. this code is successfully run in .html page. Actually I have already a .js page. When I write this code in my .js page the error occurred. this line is shown in red color. How to possible this code in .js page? – Mr.Prakash Mondal Mar 09 '18 at 10:02
  • What is the error? You have to check if it is working or not. Dont rely on you notepad/IDE. Your IDE might not be updated. `map` is a new code, so i wast thinking your IDE is not updated. – Eddie Mar 09 '18 at 10:03
  • My IDE version is IDE 8.0 – Mr.Prakash Mondal Mar 09 '18 at 10:24
  • Yes. it is successfully working in my browser (Mozila) that is: like abc.html




    – Mr.Prakash Mondal Mar 09 '18 at 10:28
  • Is it working on Mozila when you put the code on .js? – Eddie Mar 09 '18 at 10:31
  • yes. when I save this code in a .js file. and call from html file, then successfully working. – Mr.Prakash Mondal Mar 09 '18 at 10:58
  • That is great. It means your IDE is just recognizing the map code. But this should work on browsers – Eddie Mar 09 '18 at 11:00
  • No, in IDE 8.0 is not working. my .js file in IDE 8.0, When I paste this code in .js file then show red underline – Mr.Prakash Mondal Mar 09 '18 at 11:10
  • Dont mind it. The ide just don’t recognize the code. As long as it works on a browser. It is fine – Eddie Mar 09 '18 at 11:12
  • Do you help me another? [{ "name":"John", "age":30, "car":"maruti" },{ "name":"Smith", "age":50, "car":"indica" }] I want to get [{ "name":"John", "age":, "car":"maruti" },{ "name":"Smith", "age":, "car":"indica" }] Particular one value that is "age" is empty – Mr.Prakash Mondal Mar 09 '18 at 11:33
0

Here's a solution that won't change the original value:

myObj.map(x => Object.keys(x).reduce((total, value) => { total[value] = ''; return total; }, {}));
kshetline
  • 12,547
  • 4
  • 37
  • 73
  • ok fine. this code is successfully run in .html page. Actually I have already a .js page. When I write this code in my .js page the error occurred. this line is shown in red color. How to possible this code in .js page? – Mr.Prakash Mondal Mar 09 '18 at 10:03