简体   繁体   English

转换是否将基本逻辑转换为foreach失败

[英]transform if else base logic to foreach failed

I have this function 我有这个功能

const applyColor = (value) => {

        let color
        //shallow to dark
        const colors = ['#B3E5FC', '#81D4FA' ,'#4FC3F7', '#29B6F6', '#03A9F4', '#039BE5', '#0288D1']

        if(value >= 100 && value < 199){
            return {index: 1, color: colors[0]}
        }else if(value >= 200 && value < 299){
            return {index: 2, color: colors[1]}
        }else if(value >= 300 && value < 399){
            return {index: 3, color: colors[2]}
        }else if(value >= 400 && value < 499){
            return {index: 4, color: colors[3]}
        }else if(value >= 500 && value < 599){
            return {index: 5, color: colors[4]}
        }else if(value >= 600 && value < 699){
            return {index: 6, color: colors[5]}
        }else if(value >= 700){
            return {index: 7, color: colors[6]}
        }
}

It's working when I do 我在工作的时候

{data.map(o => <div style={{background: applyColor(o.value).color }}></div>)}

But I tried to refactor it using loop. 但是我尝试使用循环来重构它。

let gap = 100
        const maximum = 700
        for(let i = 0; i < colors.length; i++) {

            if(value >= maximum) {
                color = {index: colors.length, color: colors[colors.length - 1]}
            }else if(value >= gap && value < gap + 99){
                color = {index: i, color: colors[i]}
            }
            gap += 100

            return color

        }

I got color of undefined error, I couldn't spot what's wrong. 我有未定义错误的颜色,我无法发现问题所在。 Any clue? 有什么线索吗?

As others ahve mentioned your i now starts at 0 rather than 1. 正如其他人提到的那样,您的i现在从0而不是1开始。

I'm not overly familiar with javascript so excuse any syntax errors (please suggest edits if this is wrong). 我对javascript不太熟悉,因此请原谅任何语法错误(如果这是错误的,请提出建议)。 Could this not be simplified to: 不能简化为:

let color
const colors = ['#B3E5FC', '#81D4FA' ,'#4FC3F7', '#29B6F6', '#03A9F4', '#039BE5', '#0288D1']
var i = Math.floor(value/100)
if (i> colors.Length) {i= colors.Length}
if (i <1) {i= 1}
return {index: i, color: colors[i - 1]}

Basically value integer division by 100 If its over 7 make it 7 or < 1 make it 1 return the color 基本上将整数除以100,如果其大于7则使其等于7或<1使其使其等于1

You need to take care for values less than the gap and you need a consistent way to calculate the index. 您需要注意小于间隔的值,并且需要一种一致的方法来计算索引。 You current code will start at 0 ( the previous started at 1 ) but you last index will be colors.length which is 7 ( while previously it would be 6 ), so now 6 is entirely skipped. 您当前的代码将从0开始( 上一个从1开始 ),但是最后一个索引将是colors.length ,它是7以前是6 ),因此现在完全跳过了6。

Use 采用

 const applyColor = (value) => { const colors = ['#B3E5FC', '#81D4FA', '#4FC3F7', '#29B6F6', '#03A9F4', '#039BE5', '#0288D1'] const step = 100, index = Math.max(1,Math.min(Math.floor(value / step), colors.length)); // clamp index between 1 and color.length return { index: index , color: colors[index-1] } } // testing for(let i = 50; i<=850;i+=100){ console.log(`applyColor(${i}) == `, applyColor(i)); } 
 /*for stackoverflow console*/ div.as-console-wrapper{max-height:100%} 

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

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