我有一个这样的数组:
const rawdata = [
{
top_feature_col: "s9",
top_feature_row: 1,
data: [{ cluster: 0, value: 151508, opportunity_perc: 69 }]
},
{
top_feature_col: "s9",
top_feature_row: 2,
data: [{ cluster: 0, value: 127518, opportunity_perc: 70 }]
},
{
top_feature_col: "s9",
top_feature_row: 2,
data: [{ cluster: 1, value: 12668, opportunity_perc: 40 }]
}
];Run Code Online (Sandbox Code Playgroud)
我想过滤哪里,opportunity_perc >= 50但我不知道该怎么做。
结果应该是:
const result = [
{
top_feature_col: "s9",
top_feature_row: 1,
data: [{ cluster: 0, value: 151508, opportunity_perc: 69 }]
},
{
top_feature_col: "s9",
top_feature_row: 2,
data: [{ cluster: 0, value: 127518, opportunity_perc: 70 }]
}
];Run Code Online (Sandbox Code Playgroud)
您应该尝试使用类的filter方法Array:
const result = rawdata
.filter(({data}) => data[0].opportunity_perc >= 50);
Run Code Online (Sandbox Code Playgroud)
如果data数组仅包含一个元素(根据您的示例),则这将是有效的,否则您应该在此处的请求中指定行为应该是什么。