简体   繁体   English

条件 if 语句在.map

[英]Conditional if statement in .map

I would like to read the preferred_part and if it is true it should display checkmark and if it is false it won't display anything.我想阅读preferred_part,如果它是真的,它应该显示复选标记,如果它是假的,它不会显示任何东西。 This is the code I have below and the error I get is:这是我下面的代码,我得到的错误是:

TypeError: Cannot read property 'substring' of undefined. TypeError:无法读取未定义的属性“子字符串”。

I don't even properly know how I should be able to read the boolean values of preferred part in any other way.我什至不知道如何以任何其他方式读取首选部分的 boolean 值。 Any sort of help will be appreciated.任何形式的帮助将不胜感激。 Please tell me how I can solve this issue.请告诉我如何解决这个问题。 I am new to react.我是新来的反应。 Thank you!谢谢!

async function getParts() {
    console.log("Getting parts...");
    const res = await fetch("http://localhost:5000/lookup");
    const data = await res.json();
    setParts(data);
}

useEffect(() => {
    getParts();
}, []);

function formatLightState() {
    if (parts.preferred_parts = true) {
        return parts.tps_part_number + " - " + parts.manufacturer + " (" + parts.technical.substring(0, 95) + " ✔" + ")"
    }
    else {
        return parts.tps_part_number + " - " + parts.manufacturer + " (" + parts.technical.substring(0, 95) + ")"
    }
}

const partsList = parts.map((option) => ({
    manufacturer: option.manufacturer,
    technical: option.technical,
    tps_part_number: option.tps_part_number,
    value: option.part_key,
    quote_price: option.quote_price,
    vendor_name: option.vendor_name,
    vendor_id: option.vendor_key,
    label: formatLightState(),
}));

Currently parts is an array, that's why you are calling .map() there.目前,parts 是一个数组,这就是你在那里调用.map()的原因。 It does not have property technical but instead the items of the array.它没有technical属性,而是数组的项目。

Based on that you need to pass option to formatLightState which represents the current element of the iteration from your array thus you can use that inside.基于此,您需要将option传递给formatLightState ,它表示数组中迭代的当前元素,因此您可以在内部使用它。 Also instead of = you need to use === because = is used for assigning values the second one is checking equality.此外,您需要使用===而不是= ,因为=用于分配值,第二个是检查相等性。 Additionally you can use ternary operator to check if you have the preferred part so you can add the tick.此外,您可以使用三元运算符来检查您是否有首选部分,以便添加刻度。 See from the documentation :文档中查看:

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execute if the condition is falsy.条件(三元)运算符是唯一的 JavaScript 运算符,它采用三个操作数:条件后跟问号 (?),然后是条件为真时执行的表达式,后跟冒号 (:),最后是表达式如果条件不成立则执行。 This operator is frequently used as a shortcut for the if statement.此运算符经常用作 if 语句的快捷方式。

Try as the following:尝试如下:

function formatLightState(option) {
    const ending = option.preferred_parts ? ' ✔' : '';
    return option.tps_part_number + ' - ' +
           option.manufacturer +
           ' (' + option.technical.substring(0, 95) + ending + ')';
}

const partsList = parts.map((option) => ({
    manufacturer: option.manufacturer,
    technical: option.technical,
    tps_part_number: option.tps_part_number,
    value: option.part_key,
    quote_price: option.quote_price,
    vendor_name: option.vendor_name,
    vendor_id: option.vendor_key,
    label: formatLightState(option),
}));

I would also consider checking the length of option.technical because it can throw and error if the length is less than or equal 95 .我还会考虑检查option.technical的长度,因为如果长度小于或等于95 ,它可能会抛出错误。 I would check as the following:我会检查如下:

const { technical } = option;
const shortTechnical = technical.length > 95 ? technical.substring(0, 95) : technical;

I hope this helps!我希望这有帮助!

You need to wait for getParts() to finish before you can do parts.map()您需要等待 getParts( getParts()完成才能执行parts.map()

Also, this is wrong:另外,这是错误的:

if (parts.preferred_parts = true) {

You are trying to set parts.preferred_parts to true instead of checking if it's value is true.您正在尝试将parts.preferred_parts设置为 true 而不是检查它的值是否为 true。

Try This:尝试这个:

async function getParts() {
    console.log("Getting parts...");
    const res = await fetch("http://localhost:5000/lookup");
    const data = await res.json();
    await setParts(data);

    const partsList = parts.map((option) => ({
        manufacturer: option.manufacturer,
        technical: option.technical,
        tps_part_number: option.tps_part_number,
        value: option.part_key,
        quote_price: option.quote_price,
        vendor_name: option.vendor_name,
        vendor_id: option.vendor_key,
        label: formatLightState(),
    }));
}

useEffect(() => {
    getParts();
}, []);

function formatLightState() {
    if (parts.preferred_parts = true) {
        return parts.tps_part_number + " - " + parts.manufacturer + " (" + parts.technical.substring(0, 95) + " ✔" + ")"
    }
    else {
        return parts.tps_part_number + " - " + parts.manufacturer + " (" + parts.technical.substring(0, 95) + ")"
    }
}

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

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