Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
114
rated 0 times [  117] [ 3]  / answers: 1 / hits: 29901  / 10 Years ago, wed, march 5, 2014, 12:00:00

I have an object. I would like to modify the object (not clone it) by removing all properties except for certain specific properties. For instance, if I started with this object:



var myObj={
p1:123,
p2:321,
p3:{p3_1:1231,p3_2:342},
p4:'23423',
//....
p99:{p99_1:'sadf',p99_2:234},
p100:3434
}


and only want properties p1, p2, and p100, how can I obtain this object:



var myObj={
p1:123,
p2:321,
p100:3434
}


I understand how I could do this with brute force, but would like a more elegant solution.


More From » javascript

 Answers
4

You could use this approach:


let result = (({ p1, p2, p100 }) => ({ p1, p2, p100 }))(myObj);


which I learned at https://stackoverflow.com/a/25554551/470749.


[#72137] Tuesday, March 4, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ravenl

Total Points: 338
Total Questions: 107
Total Answers: 112

Location: Belize
Member since Mon, Jun 20, 2022
2 Years ago
ravenl questions
Thu, Feb 18, 21, 00:00, 3 Years ago
Tue, Jan 12, 21, 00:00, 3 Years ago
Tue, Mar 17, 20, 00:00, 4 Years ago
;