Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

break in map javascript

That's not possible using the built-in Array.prototype.map. 
However, you could use a simple for-loop instead, 
if you do not intend to map any values:

var hasValueLessThanTen = false;
for (var i = 0; i < myArray.length; i++) {
  if (myArray[i] < 10) {
    hasValueLessThanTen = true;
    break;
  }
}
Comment

break out of map javascript

That's not possible using the built-in Array.prototype.map. 
However, you could use a simple for-loop instead, if you do not intend to map any values:
var hasValueLessThanTen = false;
for (var i = 0; i < myArray.length; i++) {
  if (myArray[i] < 10) {
    hasValueLessThanTen = true;
    break;
  }
}
Comment

javascript break out of map

// You cannot break out of `Array.protoype.map`.

/*
Map is essentially a for loop that applies a
function to each value of an array and pushes it
to a new array.
*/

// A simple recreation of `Array.prototype.map`:
Array.prototype.map = function(callbackfn) {
	const result = [];
	for (let i = 0; i < this.length; i++) {
		result.push(callbackfn(result, i, this));
	}
	return result;
};

/*
Essentially, you can just create a new array and
use a for loop which you can break from:
*/
const arr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];

const arrX2 = [];
for (let i = 0; i < arr.length; i++) {
	if (i >= 5) {
		break;
	}
	arrX2.push(arr[i] * 2);
}

/*
Although if you are 100% set on 'breaking' from
`Array.prototype.map`, you could customise the
function to suit you. Have a look at this example:
*/
Array.prototype.map = function(callbackfn) {
	const result = [];
	let _break = false;
	function breakFunc() { _break = true; }
		for (let i = 0; i < this.length; i++) {
			/*
			we need to store the item so we don't
			automatically push undefined to the
			array if the break function is
			called.
			*/
			const item = callbackfn(this[i], i, this, breakFunc);
			if (_break) {
				return result;
			}
			result.push(item);
	}
	return result;
};

const arr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];

const arrX2 = arr.map((item, index, array, fakeBreak) => index >= 5 ? fakeBreak() : item);

/*
You could create your own custom `Array.prototype.map`
like the one above if you really wanted, but it is
probably better to just use a normal for loop.
*/
Comment

how to break from map in javascript

let isBroken = false;

colours.map(item => {
    if (isBroken) {
        return;
    }
    if (item.startsWith("y")) {
        console.log("The yessiest colour!");
        isBroken = true;
        return;
    }
});
Comment

Break From Map in javascript

var myArray = [22,34,5,67,99,0];
var hasValueLessThanTen = myArray.some(function (val) { 
  return val < 10;
  console.log(val)
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: Javascript: 
Javascript :: set className with ref react 
Javascript :: vuejs how use this.$slots.default 
Javascript :: initiate node js app 
Javascript :: react native stopwatch 
Javascript :: remove duplicate item from array javascript 
Javascript :: how to name a file path in document.geteleementbyid 
Javascript :: buttons js before submit 
Javascript :: javascript string to ascii array 
Javascript :: javascript dom methods list 
Javascript :: create immutable object in javascript 
Javascript :: app.js not found in laravel 8 
Javascript :: swap first and last element in array javascript 
Javascript :: set twig variable from javascript 
Javascript :: js loop array back 
Javascript :: Check if a number is even or odd 
Javascript :: nextjs use dotnenv 
Javascript :: public JsonResult what is the return 
Javascript :: sum in javascript 
Python :: tkinter how to make a root non rezizable 
Python :: suppres tensorflow warnings 
Python :: install matplotlib 
Python :: python currnent time 
Python :: python open web browser 
Python :: remove all pycache files 
Python :: python console pause 
Python :: python list all csv in dir 
Python :: python random text generator 
Python :: continue reading lines until there is no more input python 
Python :: how to import pygame onto python 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =