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
var emptyArray = [];
output.rows.map(function (row) {
row.cells.forEach(function (cell) {
if (cell.value == 'Open') {
emptyArray.push(row.id);
}
})
});
Solution 1:
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);
Solution 2:
Map the rows, within the map lambda filter the desired cells. Something like:
const filterCells = v =>
v.columnId === 8505590182897540 && v.value === `Open` ||
v.columnId === 7009559238731652 && !v.value;
const filtered = values().rows.map(v =>
( {id: v.id, cellsFound: v.cells.filter(filterCells)} ));
console.log(`if you only need ids: ${filtered.map(v => v.id)}`);
console.log(filtered);
function values() {
return {
"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"
}
]
}
]
};
}
.as-console-wrapper {
max-height: 100% !important;
}