Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to filter object of arrays based on inner object column condition

I have the following object and i want to only get the first row that is row with Number (rowNumber: 1)
The criteria is that "columnId": 8505590182897540 should have its value equal to Open and "columnId": 7009559238731652 should have its value not exist.

  output =  { "rows": [
        {
            "id": 1844195940165508,
            "rowNumber": 1,

            "cells": [
                {
                    "columnId": 8505590182897540,
                    "value": "Open",
                    "displayValue": "Open"
                },
                {
                    "columnId": 7009559238731699,
                    "value": "Steep",
                    "displayValue": "Steep"
                },
                {
                    "columnId": 7009559238731652
                }
            ]
        },
        {
            "id": 1844195940165509,
            "rowNumber": 2,

            "cells": [
                {
                    "columnId": 8505590182897540,
                    "value": "Open",
                    "displayValue": "Open"
                },
                {
                    "columnId": 7009559238731699,
                    "value": "Steep",
                    "displayValue": "Steep"
                },
                {
                    "columnId": 7009559238731652,
                    "value": "Field has value",
                    "displayValue": "Field has value"
                }
            ]
        }
    ]
}

I am able to loop through the rows with the following code and filter based on the cell.value = Open" and add the result to a list, but how can i check for each cell based on the columnid ‘8505590182897540’ if the value =="Open" and the columnid ‘7009559238731652’ where value is blank and add that row id to the list?

this is the code i have so far

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var emptyArray = [];
output.rows.map(function (row) {
    row.cells.forEach(function (cell) {
          if (cell.value == 'Open') {
              emptyArray.push(row.id);
          }
      })
    });

>Solution :

You could define filters for cell objects and check all filters.

const
    data = [{ id: 1844195940165508, rowNumber: 1, cells: [{ columnId: 8505590182897540, value: "Open", displayValue: "Open" }, { columnId: 7009559238731699, value: "Steep", displayValue: "Steep" }, { columnId: 7009559238731652 }] }, { id: 1844195940165509, rowNumber: 2, cells: [{ columnId: 8505590182897540, value: "Open", displayValue: "Open" }, { columnId: 7009559238731699, value: "Steep", displayValue: "Steep" }, { columnId: 7009559238731652, value: "Field has value", displayValue: "Field has value" }] }],
    filters = [
        { columnId: 8505590182897540, value: 'Open' },
        { columnId: 7009559238731652, value: undefined }
    ],
    result = data.filter(({ cells }) => filters.every(f => cells.some(c =>
        c.columnId === f.columnId &&
        c.value === f.value
    )));

console.log(result);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading