Filter object keys by values and map to array

javascript filter object by key value
javascript map
javascript map array of objects
javascript get value by key in array
javascript map, filter
javascript object get value by key
javascript get value by key in array of objects
javascript map values to array

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?

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>

JavaScript: manipulating objects with Object.keys() – clubmate.fi, The Object.entries() method returns an array of a given object's own of a given object's own enumerable string-keyed property [ key , value ] pairs, const map = new Map(Object.entries(obj)); console.log(map); // Map { foo:  Approach: By using Object.keys (), we are extracting keys from the Object then this key passed to map () function which maps the key and corresponding value as an array, as described in the below example.

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

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

Object.entries(), Object.entries(obj) – returns an array of [key, value] pairs. Objects lack many methods that exist for arrays, e.g. map , filter and others. If we'd  My Object: const searchFor = ['appInfo', 'questions']; My Data: const data = [ { id: '1', data: 'jobInfo' }, { id: '2', data: 'appInfo' }, { id: '3', data: 'documents

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))

Object.keys, values, entries, The filter() method creates a new array with all elements that pass the test implemented by the provided Object key-value map reversal. Objects lack many methods that exist for arrays, e.g. map, filter and others. If we’d like to apply them, then we can use Object.entries followed by Object.fromEntries: Use Object.entries(obj) to get an array of key/value pairs from obj. Use array methods on that array, e.g. map.

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);

How to simplify your codebase with map(), reduce(), and filter() in , If list is a JavaScript object, iteratee's arguments will be (value, key, list). Returns Produces a new array of values by mapping each value in list through a var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }); => [2, 4, 6]. transform object to array with lodash, You can use _. map function (of both lodash and underscore) with object as well, it will internally handle that case, iterate over each value and key with your iteratee, and finally return an array. Infact, you can use it without any iteratee (just _. map(obj)) if you just want a array of values.

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)

Underscore.js, Returns. (Array): Returns the new array of filtered values. toPairs ; this method returns an object composed from key-value pairs . Many lodash methods are guarded to work as iteratees for methods like _.every , _.filter , _.map , _. As you can see, using .reduce() is an easy way to generate a single value or object from an array. .filter() What if you have an array, but only want some of the elements in it?

template function, The first argument to the function is the value; the second argument is the key of the object property. The function can return any value to add to the array. new Map ([iterable]) – creates the map, with optional iterable (e.g. array) of [key,value] pairs for initialization. map.set (key, value) – stores the value by the key. map.get (key) – returns the value by the key, undefined if key doesn’t exist in map. map.has (key) – returns true if the key exists, false otherwise.

jQuery.map(), Where the `filters` argument is an object that contains a `key: string` 2013 }; // Convert existing array values to strings const newArr = arr.map(item => _. Use var myKeys = Object.keys(myObject) to get the keys. Check if a myString exist in the array myKeys using native. var matchingKey = myKeys.indexOf(myString) !== -1 Processing an array? Use this: var matchingKeys = myKeys.filter(function(key){ return key.indexOf(myString) !== -1 }); Get the value using myObject[matchingKey]. Processing an

Filters an array of objects with multiple match-criteria. · GitHub, Split up objects with Object.entries() and Array.filter. Tagged that will return a nested array of the objects key and values. We'll make use of not only Object.​entries(), but also the very handy array methods filter() and map() . [map/filter] Array -> Array [mapObject] Object -> Object [pluck] Object -> Array [groupBy?] Array -> Object What I mean is this: if you know a transformation is going to modify values in an array and return an array you'd probably reach for map first, if you know a transformation is going to modify values of an object, and return an object, you