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

3
  • 2
    please show what you have already done to solve the problem Commented Mar 9, 2018 at 7:30
  • stackoverflow.com/questions/23774231/… Commented Mar 9, 2018 at 7:33
  • Your output is not valid. Commented Mar 9, 2018 at 7:34

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

10 Comments

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?
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.
My IDE version is IDE 8.0
Yes. it is successfully working in my browser (Mozila) that is: like abc.html <!DOCTYPE html> <html> <body> <p id="demo1"></p><br/> <br/><br/> <p id="demo2"></p> <script> var myObj, x; myObj = [{ "name":"John", "age":30, "car":"maruti" },{ "name":"Smith", "age":50, "car":"indica" }]; var result2= myObj.map(x => Object.keys(x).reduce((total, value) => { total[value] = ''; return total; }, {})); document.getElementById("demo2").innerHTML = JSON.stringify(result2); </script> </body> </html>
Is it working on Mozila when you put the code on .js?
|
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; }, {}));

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.