Javascript .map中的条件if语句
我想阅读首选部分,如果它是真的,它应该显示复选标记,如果它是假的,它不会显示任何东西。这是我下面的代码,我得到的错误是: TypeError:无法读取未定义的属性“substring” 我甚至不知道我应该如何以任何其他方式读取首选部件的布尔值。任何形式的帮助都将不胜感激。请告诉我如何解决这个问题。我是新来的。谢谢大家!Javascript .map中的条件if语句,javascript,node.js,reactjs,react-native,Javascript,Node.js,Reactjs,React Native,我想阅读首选部分,如果它是真的,它应该显示复选标记,如果它是假的,它不会显示任何东西。这是我下面的代码,我得到的错误是: TypeError:无法读取未定义的属性“substring” 我甚至不知道我应该如何以任何其他方式读取首选部件的布尔值。任何形式的帮助都将不胜感激。请告诉我如何解决这个问题。我是新来的。谢谢大家! async function getParts() { console.log("Getting parts..."); const res = await fe
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(),
}));
您需要等待
getParts()
完成,然后才能执行parts.map()
而且,这是错误的:
if(parts.preferred\u parts=true){
您正在尝试设置
部分。首选\u parts
为true,而不是检查其值是否为true。当前部分是一个数组,这就是您在那里调用.map()
的原因。它没有属性技术属性
,而是数组的项
基于此,您需要将选项
传递给formatLightState
,该选项表示数组中迭代的当前元素,因此您可以在内部使用该元素。此外,您需要使用=
而不是==
,因为=
用于赋值,第二个选项是检查相等性。Additionally您可以使用三元运算符检查是否有首选零件,以便添加勾号。请参见:
条件(三元)运算符是唯一接受三个操作数的JavaScript运算符:一个条件后跟一个问号(?),然后是一个表达式,如果条件为truthy,则执行该表达式,后跟一个冒号(:),最后是条件为falsy时要执行的表达式。此运算符经常用作if语句的快捷方式
请尝试以下操作:
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),
}));
const { technical } = option;
const shortTechnical = technical.length > 95 ? technical.substring(0, 95) : technical;
<>我也会考虑检查<代码>选项>技术< /代码>,因为如果长度小于或等于代码>95 <代码>,它可能会抛出和出错。我将检查如下:
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),
}));
const { technical } = option;
const shortTechnical = technical.length > 95 ? technical.substring(0, 95) : technical;
我希望这有帮助!试试这个:
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) + ")"
}
}
这不是Java。对不起,我删除了它。