Javascript hasOwnProperty返回空
我需要从JSON显示一个表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)
<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