Wednesday, July 3, 2024
HomeLanguagesJavascriptLodash _.prototype.next() Method

Lodash _.prototype.next() 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.next() method of Sequence in lodash is used to find the next value on a wrapped object which follows the iterator protocol.

Syntax:

_.prototype.next()

Parameters: This method doesn’t accept any parameter.

Return Value: This method returns the value of next iterator.

Example 1:

Javascript




// Requiring lodash library
const _ = require('lodash');
  
// Initializing wrapped value
var wrapper = _([5, 6]);
  
// Calling prototype.next() method 
let res1 = wrapper.next();
let res2 = wrapper.next();
   
// Displays output
console.log(res1);
console.log(res2);


Output:

{ done: false, value: 5 }
{ done: false, value: 6 }

Example 2:

Javascript




// Requiring lodash library
const _ = require('lodash');
  
// Initializing wrapped value
var wrapper = _([]);
  
// Calling prototype.next() method 
let result = wrapper.next();
   
// Displays output
console.log(result);


Output:

{ done: true, value: undefined }

Here, the output is ‘undefined’ as there is no value to print.

Example 3:

Javascript




// Requiring lodash library
const _ = require('lodash');
  
// Calling prototype.next() method 
let result1 = _({'f':5}).next();
let result2 = _({'g':(1/0)}).next();
   
// Displays output
console.log(result1);
console.log(result2);


Output:

{ done: false, value: 5 }
{ done: false, value: Infinity }

Here, only the defined value is returned to the output.

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

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!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments