Reputation: 1836
I have an array
const sampleObject = {Color: "Blue", Size: "39, 41"}
Using Lodash's _.filter when I try
_.filter(sampleObject, (entry) => entry !== 'Blue')
I get
['39, 41']
But my desired result is
{Size: '39, 41'}
const sampleObject = {
Color: "Blue",
Size: "39, 41"
}
const filtered = _.filter(sampleObject, (entry) => entry !== 'Blue')
console.log(filtered);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
Upvotes: 1
Views: 837
Reputation: 1074495
If you want just that one property in a new object, grab it directly:
const sampleObject = {Color: "Blue", Size: "39, 41"};
const result = {Size: sampleObject.Size};
console.log(result);
Or if you need the key to be dynamic:
const sampleObject = {Color: "Blue", Size: "39, 41"};
const key = "Size";
const result = {[key]: sampleObject[key]};
console.log(result);
Upvotes: 0
Reputation: 20486
Are you looking for _.pickBy()
?
const sampleObject = {
Color: "Blue",
Size: "39, 41"
}
const filtered = _.pickBy(sampleObject, (value,key) => value !== 'Blue')
console.log(filtered);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
Upvotes: 3
Reputation: 207521
Seems odd you need to do this, but you can use fromEntries and entries to do it
const sampleObject = {Color: "Blue", Size: "39, 41"}
const result = Object.fromEntries(Object.entries(sampleObject).filter(x => x[1] !== 'Blue'));
console.log(result);
Upvotes: 0