Skip to content
Advertisement

Move specific properties from object to object

I have an object with a lot of properties, lets say:

JavaScript

I want to separate a group of properties as follow:

JavaScript

What i am doing is:

JavaScript

is this the only way to achive this? is there a way to ignore properties that are undefined? For example let say h is undefined, so ideally obj2 would only be obj2={a,w} It is possible that in the future obj1 will grow and that means i will have to add the new properties in both lines of my approach.

Advertisement

Answer

It’s not the only way, but the other ways have the same problem that you noted: You have to code the property names, and so if you add more properties later, you have to add those. Either way, you have to have a list of either A) the properties you want in obj2 or B) the properties you want in obj3.

Just for illustration, here’s one other way, but I’m not saying it’s at all better:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement