简体   繁体   English

过滤对象数组并删除对象是过滤器返回空值

[英]Filter array of objects and remove object is filter returns empty values

Here is my question.这是我的问题。 I have an array of objects as shown below.我有一个对象数组,如下所示。 Each object has a title and rows in it.每个对象都有一个标题和行。 rows is also array of objects with risk values like P1, P2, P3 etc. I want to filter and get only those rows whose risk is equal to P1 . rows也是具有risk值的对象数组,例如 P1、P2、P3 等。我想过滤并仅获取risk等于P1的那些行。 In some objects, there might be no risk with P1 at all.在某些对象中,P1 可能根本没有风险。 In that case, i want to ignore that object completely.在这种情况下,我想完全忽略该对象。

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",
            }
        ]
    }
]

I want the result as follows.我想要的结果如下。 As you can see the title QA didnt have P1 at all.如您所见,标题QA根本没有 P1。 so i want to ignore that object as a whole.所以我想忽略整个对象。

    {
        "title": "Prod",
        "rows": [
            {
                "risk": "P1",
                "Title": "Server is in hung state",
            }
        ]
    }
]

I tried using this code.我尝试使用此代码。 but the problem with this is that the title with QA gets added in the final output even though it doesnot have any P1 risk in it.但是这样做的问题是,带有 QA 的标题会被添加到最终输出中,即使它没有任何 P1 风险。

let result = input.map((obj) => ({
  ...obj,
  rows: obj.rows.filter((row) => row.risk === "P1"),
}));

Can someone let me know how to filter the objects with P1 and skip the whole object if there are no P1 in it.有人可以让我知道如何使用 P1 过滤对象,如果其中没有 P1,则跳过整个对象。

You can filter out those objects without risk === "P1" by using the function Array.prototype.filter .您可以使用函数Array.prototype.filter过滤掉那些没有风险的对象 === "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", } ] }], 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; }

I would skip the map entirely and only use a filter:我会完全跳过地图,只使用过滤器:

const result = data.filter((obj) => {
  let includesP1 = false;
  obj.rows.forEach((r) => {
    if (r.risk === "P1") includesP1 = true;
  });
  return includesP1;
});

You can build up a new list based on those which have rows with P1 risk.您可以根据具有 P1 风险的行建立一个新列表。

data.reduce((acc, item) => {
  const rows = item.rows.filter(it => it.risk === 'P1');
  if (rows.length === 0) return acc;
  return [...acc, {...item, rows }]
}, [])

Using reduce you can filter out all non P1 risks.使用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);

Another approach另一种方法

You can also first filter the data that have at least one P1 risk and then from the filtered data remove all the non P1 risks.您也可以先过滤至少有一个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);

Relevant documentations:相关文件:

This could be done as followed:这可以按如下方式完成:


  // 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM