Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
&&;运算符在JavaScript中返回0_Javascript - Fatal编程技术网

&&;运算符在JavaScript中返回0

&&;运算符在JavaScript中返回0,javascript,Javascript,我有一个这样的对象数组 const items = [{ label : "foo" value : 1 }, { label : "bar" value : 2 },] 当我有value1=1 value2=2 value3=0时,执行此代码 [value1 && items.find(({value}) => value === value1).label, value2 && items.find(({va

我有一个这样的对象数组

  const items = [{ label : "foo" value : 1 },
                 { label : "bar" value : 2 },]
当我有
value1=1 value2=2 value3=0
时,执行此代码

[value1  && items.find(({value}) => value === value1).label,
 value2  && items.find(({value}) => value === value2).label,
 value3  && items.find(({value}) => value === value3).label,]
它返回结果
[“foo”,“bar”,0]
,0显示在屏幕上。 我想要的是空字符串,而不是像这样的0
我如何才能做到这一点?

您可以使用默认值

const
    items = [{ label: "foo" value: 1 }, { label: "bar" value: 2 }],
    result = [1, 2, 3].map(v =>
        v && state.items.find(({ value }) => value === v)?.label || '');

可以采用默认值

const
    items = [{ label: "foo" value: 1 }, { label: "bar" value: 2 }],
    result = [1, 2, 3].map(v =>
        v && state.items.find(({ value }) => value === v)?.label || '');
你可以代替

value3 && items.find(({value}) => value === value3).label

你可以代替

value3 && items.find(({value}) => value === value3).label


你希望
从何而来?
值3和&items.find({value})=>value==value3.label | |“
@palaѕ如果找不到则不起作用。你希望
从何而来?
值3和&items.find({value}=>value==value3.label |
@palaѕ如果找不到则不起作用。