How do I get the key where a property of the object is true?

javascript object get value by key
object.keys foreach
javascript get object properties
typescript object keys
javascript object contains value
javascript get value by key in array
add property to object javascript
key/value pair javascript

I'm using lodash and trying to figure out how to get the key name of the object that has the valid property set to true.

 this.booking = {
    personal:{ valid:false, data:{ } },
    listing:{ valid:true, data:{ } },
    cart:{ valid:false, data:{ } },
    checkout:{ valid:true, data:{ } }
  }

const result = _.map(this.booking, 'valid')
console.log(result) // returns [false, true, false, true]

How would I write a lodash function that would return ['listing', 'checkout] based on the object above?

Use _.pickBy() to filter the object, to valid properties, and then get the keys with _.keys() (or Object.keys()):

const booking = {
  personal:{ valid:false, data:{ } },
  listing:{ valid:true, data:{ } },
  cart:{ valid:false, data:{ } },
  checkout:{ valid:true, data:{ } }
}

const result = _.keys(_.pickBy(booking, 'valid'));

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

How to get object keys for true values in JavaScript, Explanation of how to get object keys for true value in JavaScript, using method returns an array of a given object's own property names. let obj = { 1:true, 2:false, 3:true } let check = Object.values(obj).some(value => value == true) console.log(check); So, the algorithm checks if obj contains a given key and any value that is true , as opposed to if the value for a given key is true.

You don't need Lodash for this:

Object.keys(this.booking)
  .filter((key) => this.booking[key].valid);

// returns ['listing', 'checkout']

Objects, A property has a key (also known as “name” or “identifier”) before the colon ":" and a value to the right of it. In the user  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. Use Object.fromEntries(array) on the resulting array to turn it back into an object.

Or, using reduce you can do it in a single iteration since lodash#reduce can be applied in object directly.

_.reduce(booking, (r, e, k) => (e.valid && r.push(k), r), [])

Working demo

let bookings = {"personal":{"valid":false,"data":{}},"listing":{"valid":true,"data":{}},"cart":{"valid":false,"data":{}},"checkout":{"valid":true,"data":{}}},
    res = _.reduce(bookings, (r, e, k) => (e.valid && r.push(k), r), []);
    
    console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

Object.keys(), The Object.keys() method returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop  When this property is set to true, the form will receive all KeyPress, KeyDown, and KeyUp events. After the form's event handlers have completed processing the keystroke, the keystroke is then assigned to the control with focus. For example, if the KeyPreview property is set to true and the currently selected control is a TextBox, after the keystroke is handled by the event handlers of the form the TextBox control will receive the key that was pressed.

There are more than few ways to do this for example here are few options with _.pickBy, _.omitBy, and _.transform:

const obj = { personal:{ valid:false, data:{ } }, listing:{ valid:true, data:{ } }, cart:{ valid:false, data:{ } }, checkout:{ valid:true, data:{ } } }

console.log(_.keys(_.pickBy(obj, 'valid'))) 
console.log(_.keys(_.omitBy(obj, {valid: false})))
console.log(_.keys(_.transform(obj,(a, {valid}, k) => valid ? a[k] = valid : null)))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

Object.prototype.hasOwnProperty(), hasOwnProperty returns true even if the value of the property is null or undefined . o = new Object(); o.propOne =  The Object.keys() method returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop would. The source for this interactive example is stored in a GitHub repository.

Declared Properties, readOnly: true, notify: true } }, ready: function() { this.textContent = 'Hello World, I am a Custom Element!'; } });. The properties object supports the following keys  The keypressed method uses the KeyChar property to check whether the ENTER key pressed. If the ENTER key is pressed, the Handled property is set to true, which indicates the event is handled.

14. New OOP features besides classes, Remember that there are two ways of specifying a key when you set a property. Via a fixed name: obj.foo = true;; Via an expression: obj['b'+'ar'] = 123;. In object  To get the symbols of an object to enumerate over, use Object.getOwnPropertySymbols (this function is why Symbol can’t be used to make private properties). The new Reflect API from ECMAScript 2015 provides Reflect.ownKeys, which returns a list of property names (including non-enumerable ones) and symbols.

Expert VB 2005 Business Objects, It also represents the object's unique primary key value in the database: <System.​ComponentModel.DataObjectField(True, True)> _ Public ReadOnly Property  The PropertyTable class provides all the operations of PropertyBag, as well as an indexer property: public object this [string key] { get; set; } This indexer is used to get and set property values that are stored in the table's internal hashtable. Properties are indexed by name. PropertySpec Class

Comments
  • without lodash: Object.keys(this.booking).filter(b => b.valid)
  • @epascarello It should be an answer. An accepted answer :)
  • @Victor without the bug..... lol b => this.booking[b].valid lol.... I need more coffee