I have an object js that contains other objects like this :
const USER: {
LANGUAGE: {
'editable': 1,
'decimal': 0,
'degree': 0,
'master': 1,
0: ['', '', 0],
1: ['', '', 1],
},
LUM: {
'editable': 1,
'decimal': 0,
'degree': 0,
'master': 0,
0: ['', '', 0],
1: ['', '', 1],
2: ['', '', 2],
},
SP: {
'editable': 1,
'decimal': 0,
'degree': 0,
'master': 1,
0: ['', '', 0],
1: ['', '', 1],
2: ['', '', 2],
},
},
};
I want to create another object that contains only object which contains ‘master’: 1;
How can I do that?
4
Answers
JavaScript has the methods
Object.keys()
andArray.reduce()
which you can use to filter the objects based on their properties:You can try the following solutions:
or the approach using
Object.keys
and the array methodsfilter
andreduce
.An one-liner with
Object.entries()
:If you want more speed, remove the destructuring: