简体   繁体   English

跳出地图

[英]Break out of map

so i have this problem where if the value in the array is higher than entered value it should do something and then stop the loop and don't touch the remaining values in the array.. Here's the code so far:所以我有这个问题,如果数组中的值高于输入的值,它应该做一些事情然后停止循环并且不要触摸数组中的剩余值..这是到目前为止的代码:

const percentages = [];
let enteredValue = parseInt(event.target.value, 10);

range.map((rangeValue, i) => {
  if (rangeValue <= enteredValue) {
    percentages.push(100);
    enteredValue = enteredValue - rangeValue;
  } else {
    percentages.push(enteredValue * 100 / (rangeValue));

    return;
  }
});

Using .some you can get iteration functionally similar to .forEach , map or for loop but with the ability to break through return instead.使用.some您可以获得类似于.forEachmapfor循环的迭代功能,但具有break return的能力。

 range.some(function(rangeValue , i) { if (rangeValue <= enteredValue) { percentages.push(100); enteredValue = enteredValue - rangeValue; return true } percentages.push(enteredValue * 100 / (rangeValue)); });

Read more about .some in es6 here此处阅读有关.some中的es6更多信息

Just use a good old for loop:只需使用一个很好的 for 循环:

 const percentages = [];
 let enteredValue = parseInt(event.target.value, 10);

 for(const range of ranges) {
   percentages.push(Math.min(100, (enteredValue / range) * 100));
   enteredValue -= range;
   if(enteredValue <= 0) break;
 }

The other answers are perfectly sufficient.其他答案完全足够。 However, I would like to add that the map function isn't best used for performing iterations.但是,我想补充一点, map函数不适合用于执行迭代。 What the map function does is perform the as argument passed in callback on every element of the array. map函数所做的是对数组的每个元素执行回调中传递的 as 参数。 It then returns the new array .然后返回新数组 So map is more usefull when you are in need of a mutated copy of the Array (eg all the elements multiplied by 10).因此,当您需要数组的变异副本(例如,所有元素乘以 10)时,map 更有用。

For your purposes other logic like a regular for loop will be a more elegant solution.出于您的目的,其他逻辑(如常规 for 循环)将是更优雅的解决方案。

You can use reduce instead of map .您可以使用reduce而不是map The reduce senses a change in the original array. reduce感知原始数组的变化。

let enteredValue = parseInt(event.target.value, 10);

const percentages = range
    .slice(0) // if you don't want to mess up the original array
    .reduce((percentages, rangeValue, _, rangeCopyArray) => {
        if (rangeValue <= enteredValue) {
            percentages.push(100);
            enteredValue -= rangeValue;
        } else {
            percentages.push(enteredValue * 100 / rangeValue);
            rangeCopyArray.length = 0; // the loop ends here
        }
        return percentages;
    }, [])

or shorter but less clear或更短但不太清楚

const percentages = [...range].reduce(({p, eV}, rV, _, arr) => ({
    eV: eV-rV, 
    p: [...p, rV <= eV ? 100 : eV*100/rV + (arr.length = 0)]
}), {p: [], eV: parseInt(event.target.value, 10)}).p;

Break out from map - works using return从地图中突破 - 使用return

let hasId = false;
let data = {
    id: 1,
    title: 'some item'
}
let cartArray = [{
    id: 1,
    quantity: 19
}];
if (cartArray.length > 0) {
    cartArray.map(item => {
        if (item.id === data['id']) {
            ++item.quantity;
            hasId = true
            return; // Break out
        }
    })

    if (!hasId) {
        cartArray.push(data)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM