5

I am creating an angular application and I have items with checkboxes. When a user clicks on a checkbox I record checked items to the object. The object looks like that:

{1: false, 7: true, 8: true};

When a user clicks on the delete button I need to get only selected items ids.

So I need to filter objects by values and as a result, get an array of integers.

I tried the following code with the lodash library:

console.log(_.pick(this.selectedItems, _.identity));

return _.pick(this.selectedItems, function (value, key) {
        return value;
      });

But this returns an empty array.

What I need to get is an array [7,8]

What is wrong with my code?

6 Answers 6

2

use _.pickBy and use _.keys then to get keys of filtered objects.

var obj = {
  1: false,
  7: true,
  8: true,
};


var res = _.keys(_.pickBy(obj, function(value, key) {return value;}))
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.5.1/lodash.js"></script>

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

Comments

1

You could achieve this without lodash by using the native filter function

let vals = Object.keys(this.selectedItems).filter(k => this.selectedItems[k] == true)

Comments

1

Iterate the object's keys

var obj = {
  1: false,
  7: true,
  8: true,
};

var filteredObj = Object.keys(obj).reduce((p, c) => {    
  if (obj[c]) p[c] = obj[c];
  return p;
}, {});

console.log(Object.keys(filteredObj))

Comments

1

Just in case you're only using lodash for this purpose and not anywhere else in the project, here's a solution using only built-in methods (Object.keys() in conjunction with Array.prototype.filter()):

const selectedItems = {
  1: false,
  7: true,
  8: true
};

const deleteIds = Object.keys(selectedItems).filter((item) => {
  return selectedItems[item] === true
});
console.log(deleteIds);

Comments

1

You could use reduce to achieve what you want:

const data = {1: false, 7: true, 8: true};

const filtered = Object.entries(data).reduce((acc, [key, value]) => {
  if (value) return [...acc, key];
  return acc;
}, [])


console.log(filtered)

Comments

0

With lodash you need to use _.pickBy() instead of _.pick() because _.pick() doesn't accept a callback.

This is a function generated by _.flow() that will get the keys with true value, and convert them to numbers:

const { flow, partialRight: pr, pickBy, identity, keys, map } = _

const fn = flow(
  pickBy, // get the items by identity - this will remove keys with false values
  keys, // get the keys
  pr(map, Number) // map to numbers
);

const selectedItems = {1: false, 7: true, 8: true};

const result = fn(selectedItems);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

And a lodash/fp terser version:

const { flow, pickBy, identity, keys, map } = _

const fn = flow(
  pickBy(identity), // get the items by identity - this will remove keys with false values
  keys, // get the keys
  map(Number) // map to numbers
);

const selectedItems = {1: false, 7: true, 8: true};

const result = fn(selectedItems);

console.log(result);
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>

Comments

Your Answer

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