Scrolling through JavaScript object properties using Lodash

Is it possible to scroll through the properties of a JavaScript object? For example, I have a JavaScript object that is defined as follows:

myObject.options = { property1: 'value 1', property2: 'value 2' }; 

Properties will be dynamically added to this object. Is there a way for me to just go through and check if a property exists? If so, how?

+109
javascript lodash
Jan 23 '14 at 2:27
source share
7 answers

Yes it is possible and the lodash is not needed ... i.e.

 for (var key in myObject.options) { // check also if property is not inherited from prototype if (myObject.options.hasOwnProperty(key)) { var value = myObject.options[key]; } } 

Edit : the accepted answer ( _.forOwn () ) should be https://stackoverflow.com/a/464829/

+30
Jan 23 '14 at 2:30
source share

Use _.forOwn() .

 _.forOwn(obj, function(value, key) { } ); 

https://lodash.com/docs#forOwn

Note that forOwn checks hasOwnProperty , as you usually need to do when navigating through object properties. forIn does not perform this check.

+567
Jan 23 '14 at 2:31 on
source share

For your stated desire to "check if a property exists," you can directly use Lo-Dash has .

 var exists = _.has(myObject, propertyNameToCheck); 
+14
Jan 23 '14 at
source share

Take an object below as an example.

 let obj = { property1: 'value 1', property2: 'value 2'}; 

First enter the whole key in obj

 let keys = Object.keys(obj) //it will return array of keys 

and then scroll it

 keys.forEach(key => //your way) 

just all together

 Object.keys(obj).forEach(key=>{/*code here*/}) 
+2
Dec 03 '18 at 8:24
source share

In ES6, you can also iterate over object values โ€‹โ€‹using the for..of loop. However, this does not work directly from the window for JavaScript objects, since you must define the @@ iterator property on the object. This works as follows:

  • The for..of loop for..of for an โ€œiteration of the objectโ€ (let it call the obj1 object for the iterator object. The loop iterates over obj1, sequentially calling the next () method on the provided iterator object and using the return value as the value for each iteration of the loop.
  • An iterator object is obtained by calling the function defined in the @it iterator property or the Symbol.iterator property, obj1. This is a function that you must define yourself, and it must return an iterator object

Here is an example:

 const obj1 = { a: 5, b: "hello", [Symbol.iterator]: function() { const thisObj = this; let index = 0; return { next() { let keys = Object.keys(thisObj); return { value: thisObj[keys[index++]], done: (index > keys.length) }; } }; } }; 

Now we can use the for..of :

 for (val of obj1) { console.log(val); } // 5 hello 
+1
Jun 07 '17 at 13:02 on
source share

It would be helpful to understand why you need to do this with lodash. If you just want to check if a key exists in an object, you do not need lodash.

 myObject.options.hasOwnProperty('property'); 

If you want to see if a value exists, you can use _.invert

 _.invert(myObject.options)[value] 
0
Oct 02 '15 at 14:42
source share

You can definitely do this with vanilla JS, as stecb has shown, but I think each is the best answer to the basic question of how to do this with lodash.

 _.each( myObject.options, ( val, key ) => { console.log( key, val ); } ); 

As JohnnyHK mentioned, there is also a has method that will be useful for a use case, but from what was originally stated, set might be more useful. Suppose you want to add something to this object dynamically, as you mentioned:

 let dynamicKey = 'someCrazyProperty'; let dynamicValue = 'someCrazyValue'; _.set( myObject.options, dynamicKey, dynamicValue ); 

Here's how I do it based on the original description.

0
Jul 25 '19 at 14:30
source share



All Articles