Lodash _.prototype.plant() Method

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.

The _.prototype.plant(value) method of Sequence in lodash is used to create a clone of the chain sequence type by planting the value to be planted as the wrapped value.

Syntax:

_.prototype.plant(value)

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

  • value: It is the value that need to be planted.

Return Value: This method returns the new lodash wrapper instance.



Example 1:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring lodash library
const _ = require('lodash');
  
// Creating an addition function
function add(n) {
  return n + n;
}
  
// Creating wrapped value
var wrapper = _([4, 4]).map(add);
  
// Calling prototype.plant(value) method
var res = wrapper.plant([8, 8]);
   
// Displays output 
console.log(res.value());
wrapper.value();

chevron_right


Output:

[ 16, 16 ]
[ 8, 8 ]

Example 2:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring lodash library
const _ = require('lodash');
  
// Creating a function
function fun(n) {
  return (n * 5);
}
  
// Calling prototype.plant(value) method
var obj = _().map(fun).plant({'f': 5, 'g': 7});
   
// Displays output 
console.log(obj.value());

chevron_right


Output:

[ 25, 35 ]

Here, the values are multiplied by 5 and then returned as output.

Reference: https://lodash.com/docs/4.17.15#prototype-plant

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.