Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.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 hasOwnProperty返回空_Javascript_Reactjs - Fatal编程技术网

Javascript hasOwnProperty返回空

Javascript hasOwnProperty返回空,javascript,reactjs,Javascript,Reactjs,我需要从JSON显示一个表 <th> { x.amountForQuantity.filter((remaining) => { return remaining.hasOwnProperty(cost.key); })[cost.key] } </th> { x、 amountForQuantity.filter((剩余)=>{ 返回剩余的.hasOwnProperty(cost.key)

我需要从JSON显示一个表

<th>
    {
        x.amountForQuantity.filter((remaining) => {
            return remaining.hasOwnProperty(cost.key);
        })[cost.key]
    }
</th>

{
x、 amountForQuantity.filter((剩余)=>{
返回剩余的.hasOwnProperty(cost.key);
})[成本.关键]
}
上面的标签返回空,我在这里做错了什么


沙盒

您的数组结构如下

const row = [
...
      amountForQuantity: [
        {
          key: "Labour Cost",
          value: 150
        },
        {
          key: "Material Cost",
          value: 570
        }]
    ...
  ];
因此,
cost.key
不是对象的属性,而是
key
属性的

所以你的代码应该是这样的

x.amountForQuantity.filter((remaining) => remaining.key == cost.key)[0].value

filter
返回一个数组。为什么要从该数组中访问
cost.key
索引
cost.key
具有字符串值,如
“人工成本”
需要在表格中显示您需要使用
find
并更改条件:
x.amountForQuantity.find(a=>a.key==cost.key)?.value