How can i filter lines where Name value is empty in multi dimensional array.
In the following array IDs 11910008, 9980000000, 9980000010 has no name, its just a empty string so I need to filter them.
I have tried this but no luck so far;
const rows =
// [
// ['ID', 'Name']
// ];
[
[
[
"11300273",
"Domestic Supplier"
],
[
"11411110",
"Plant Business Partner"
],
[
"11910008",
""
],
[
"54009145",
"Slink"
],
[
"9980000000",
""
],
[
"9980000010",
""
],
[
"JUSTINQ",
"JUSTINQ Inc."
]
]
]
var filtered = rows.filter(row => row.join("") !== "").map(row => row.filter((cel) => cel));
console.log(filtered)
5
Answers
Test it’s empty like this
If you want to rule out the values having a null name, then it could be achieved via changing out the filter method with this:
Fully modified code:
You can use find to check if any empty string is found inside your array:
Based on what’s provided, try:
In this updated code, the filter() method is used on rows[0] (the nested array within the main array) to iterate over each inner array. The row[1] access the second element of each inner array, which corresponds to the Name value. If the Name value is not an empty string, the inner array is included in the filtered result.
You’ve been given the answer in a comment already…