Tuesday, September 24, 2024
Google search engine
HomeLanguagesJavascriptLodash _.prototype.plant() Method

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




// 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();


Output:

[ 16, 16 ]
[ 8, 8 ]

Example 2:

Javascript




// 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());


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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments