這是我的問題。我有一個物件陣列,如下所示。每個物件都有一個標題和行。rows也是具有riskP1、P2、P3 等值的物件陣列。我想過濾并僅獲取那些risk等于P1. 在某些物件中,P1 可能根本沒有風險。在這種情況下,我想完全忽略該物件。
let data = [
{
"title": "QA",
"rows": [
{
"risk": "P3",
"Title": "Permission issue",
}
]
},
{
"title": "Prod",
"rows": [
{
"risk": "P5",
"Title": "Console log errors fix",
},
{
"risk": "P1",
"Title": "Server is in hung state",
}
]
}
]
我想要的結果如下。如您所見,標題QA根本沒有 P1。所以我想忽略整個物件。
{
"title": "Prod",
"rows": [
{
"risk": "P1",
"Title": "Server is in hung state",
}
]
}
]
我嘗試使用此代碼。但是這樣做的問題是,帶有 QA 的標題會被添加到最終輸出中,即使它沒有任何 P1 風險。
let result = input.map((obj) => ({
...obj,
rows: obj.rows.filter((row) => row.risk === "P1"),
}));
有人可以讓我知道如何使用 P1 過濾物件,如果其中沒有 P1,則跳過整個物件。
uj5u.com熱心網友回復:
您可以根據具有 P1 風險的行建立一個新串列。
data.reduce((acc, item) => {
const rows = item.rows.filter(it => it.risk === 'P1');
if (rows.length === 0) return acc;
return [...acc, {...item, rows }]
}, [])
uj5u.com熱心網友回復:
您可以使用函式過濾掉那些沒有風險===“P1”的物件Array.prototype.filter。
const data = [ { "title": "QA", "rows": [ { "risk": "P3", "Title": "Permission issue", } ] }, { "title": "Prod", "rows": [ { "risk": "P5", "Title": "Console log errors fix", }, { "risk": "P1", "Title": "Server is in hung state", } ] }],
result = structuredClone(data).filter(o => {
o.rows = o.rows.filter(({risk}) => risk === "P1");
return !!o.rows.length;
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
uj5u.com熱心網友回復:
我會完全跳過地圖,只使用過濾器:
const result = data.filter((obj) => {
let includesP1 = false;
obj.rows.forEach((r) => {
if (r.risk === "P1") includesP1 = true;
});
return includesP1;
});
uj5u.com熱心網友回復:
使用reduce您可以過濾掉所有非 P1 風險。
const data = [
{ title: "QA", rows: [{ risk: "P3", Title: "Permission issue" }] },
{
title: "Prod",
rows: [
{ risk: "P5", Title: "Console log errors fix" },
{ risk: "P1", Title: "Server is in hung state" },
],
},
];
const result = data.reduce((acc, d) => {
const rows = d.rows.filter(({ risk }) => risk === "P1");
if (rows.length) {
acc.push({ ...d, rows });
}
return acc;
}, []);
console.log(result);
另一種方法
您也可以先過濾至少有一個P1風險的資料,然后從過濾后的資料中洗掉所有非P1風險。
const data = [
{ title: "QA", rows: [{ risk: "P3", Title: "Permission issue" }] },
{
title: "Prod",
rows: [
{ risk: "P5", Title: "Console log errors fix" },
{ risk: "P1", Title: "Server is in hung state" },
],
},
];
const result = data
.filter(({ rows }) => rows?.some(({ risk }) => risk === "P1"))
.map((d) => ({ ...d, rows: d.rows.filter(({ risk }) => risk === "P1") }));
console.log(result);
相關檔案:
- Array.prototype.reduce
- Array.prototype.filter
- Array.prototype.push
- Array.prototype.some
- Array.prototype.map
uj5u.com熱心網友回復:
這可以按如下方式完成:
// function to check if a row has risk P1
const hasRiskP1 = (row) => { return row.risk === 'P1' };
// first filter out all rows which do not contain P1
// then filter out all entries which do not have any rows left
const result = data.map(entry => ({...entry, ...{rows: entry.rows.filter(hasRiskP1)}}))
.filter(entry => entry.rows.length);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/494980.html
標籤:javascript 数组 json 循环 目的
