Lodash _.isAssociative() Method

The Lodash _.isAssociative() Method checks whether the given value is associative or not. An associative object is that in which its elements can be accessed via a key or index. Exp, Arrays.

Syntax:

_.isAssociative(value);

Parameters: This method accepts a single parameter as mentioned above and described below:

  • value: Given value to be checked for associative.

Return Value: This method returns a Boolean value(Returns true if the given value is associative, else false).

Note: To execute the below examples, you have to install the lodash-contrib library by using this command prompt and execute the following command.



npm install lodash-contrib

Below examples illustrate the Lodash _.isAssociative() method in JavaScript:

Example 1: Method returning true for an array.

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Checking
console.log("The Value is Associative : " 
            +_.isAssociative([1,2,3,4]));

chevron_right


Output:

The Value is Associative : true

Example 2: Method returning false for an integer.

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Checking
console.log("The Value is Associative : " 
             +_.isAssociative(2));

chevron_right


Output:

The Value is Associative : false

Example 3: For a mapping, this method returns true as its value can be accessed by keys.

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Checking
console.log("The Value is Associative : " 
             +_.isAssociative({1:2, 3:3}));

chevron_right


Output:

The Value is Associative : true

Example 4: Method returning false for a string variable.

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Checking
console.log("The Value is Associative : " 
             +_.isAssociative("GeeksforGeeks"));

chevron_right


Output:

The Value is Associative : false

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.