-1

I have the following forEach loop to iterate over stored jQuery selectors:

contentExtras = [
   { right: $right_mask, start: aVariable, end: 0 },
   { right: $le, start: 0, end: bVariable },
   { left: $left_mask, start: cVariable, end: 0 }
];

Now I can do:

contentExtras.forEach( function( item ) {
    item.right.css( { top: item.start } );
} );

or:

contentExtras.forEach( function( item ) {
    item.left.css( { bottom: item.start } );
} );

Now I am looking for a way to do get the value of every first key/value pair independent of wether it is "right" or "left".

In the way that I could do something like:

contentExtras.forEach( function( item ) {
    item.["both left and right"].css( { bottom: item.start } );
} );

I tried things like: item.[0] but without success. Does this explain sufficiently what I need? Thanks in advance for help!

EDIT:

So this is the part of the answer of Rayon that solved my problem:

contentExtras.forEach( function( item ) {
    item[Object.keys(item)[0]].doThingsWithItsValues()
} );
Garavani
  • 755
  • 1
  • 13
  • 28
  • 3
    Possible duplicate of [Access the first property of an object](http://stackoverflow.com/questions/983267/access-the-first-property-of-an-object) – rrk May 29 '16 at 09:09
  • You are probably right :-( but I cannot get ahead of this anyhow. I am sorry, didn’t see the other question or could not see the similarity. Sorry again for that. – Garavani May 29 '16 at 09:20
  • I learn: do not ask questions when you are not able to handle the answers. Or better do not ask questions when you don't know the answers. – Garavani May 29 '16 at 09:37

1 Answers1

1

Use Object.keys()

Or just ||(Logical OR) operators

contentExtras = [{
  right: '$right_mask',
  start: '-rightX',
  end: 0
}, {
  right: '$le',
  start: 0,
  end: '-leX'
}, {
  left: '$left_mask',
  start: '-rightX',
  end: 0
}];

contentExtras.forEach(function(item) {
  var key = Object.keys(item)[0]; //Get the first key
  console.log(item[key])
    //OR
  console.log(item.left || item.right);
});
Rayon
  • 36,219
  • 4
  • 49
  • 76
  • Thanks first of all! So I tried to do: contentExtras.forEach( function( item ) { Object.keys(item)[0].myFunction(); } ); but get the error:TypeError: Object.keys(item)[0].myFunction is not a function. (In 'Object.keys(item)[0].myFunction()', 'Object.keys(item)[0].myFunction' is undefined) – Garavani May 29 '16 at 09:08
  • Sorry. I am not so fast. Put some function for the jQuery selector I’d like to retrieve. – Garavani May 29 '16 at 09:11
  • @Garavani, Unable to get you :( – Rayon May 29 '16 at 09:13
  • No wonder! I am sorry. I’ll edit my question and try to get clearer! Moment! – Garavani May 29 '16 at 09:14
  • @Garavani, If you could access the property(`$right_mask/$left_mask`), What is the concern ? – Rayon May 29 '16 at 09:20
  • I tried to be not specific in my question to come to the point, but I see I am not able to explain. Sorry for that. Forget about it! I’ll find another way. – Garavani May 29 '16 at 09:24
  • The problem seems to be the ' ' that I cannot have for my values the way my code works. I need the selectors without ' '. Your code is cool but I need the value of the first key each time not the key. – Garavani May 29 '16 at 09:56
  • @Garavani I added quotes to demonstarate how it works.. You are suppose to understand than just copy paste it.. Those variables were undefined in your provided script.. – Rayon May 29 '16 at 09:58
  • Can you extend your code that it gives me the **value** of each first key (so in my case the stored jQuery selector) not the key itself? – Garavani May 29 '16 at 10:03
  • @Garavani execute the snippet, you will get the VALUE in console..(console.log statements results the value) – Rayon May 29 '16 at 10:07
  • I am sorry. No, it does not work for my case. I can do: contentExtras.forEach( function( item ) { item.right.someFunction(); } ); but I get errors when doing: contentExtras.forEach( function( item ) { Object.keys(item)[0].someFunction(); } ); – Garavani May 29 '16 at 10:11
  • function( item ) { item[Object.keys(item)[0]].someFunction(); } – Rayon May 29 '16 at 10:12
  • Bingo! Thanks a lot for your patience, Rayon! Genius! – Garavani May 29 '16 at 10:15
  • 1
    @Garavani You did test my patience mate Glad to help you! – Rayon May 29 '16 at 10:16