skip to Main Content

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


  1. Test it’s empty like this

    var filtered = rows[0].filter(row => row[1].trim() !== "")
    
    const rows = [
      [
        [
          "11300273",
          "Domestic Supplier"
        ],
        [
          "11411110",
          "Plant Business Partner"
        ],
        [
          "11910008",
          ""
        ],
        [
          "54009145",
          "Slink"
        ],
        [
          "9980000000",
          ""
        ],
        [
          "9980000010",
          ""
        ],
        [
          "JUSTINQ",
          "JUSTINQ Inc."
        ]
      ]
    ];
    var filtered = rows[0].filter(row => row[1].trim() !== "")
    console.log(filtered)
    Login or Signup to reply.
  2. let filteredArr = rows[0].filter(item => item[1] !== '');
    
    console.log(filteredArr);
    
    Login or Signup to reply.
  3. 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:

    var filtered = rows.map(row=>row.filter(data=>data[1]!==''))
    

    Fully modified code:

    const rows = 
    //    [
    //      ['ID', 'Name']
    //    ];
    
    [
      [
        [
          "11300273",
          "Domestic Supplier"
        ],
        [
          "11411110",
          "Plant Business Partner"
        ],
        [
          "11910008",
          ""
        ],
        [
          "54009145",
          "Slink"
        ],
        [
          "9980000000",
          ""
        ],
        [
          "9980000010",
          ""
        ],
        [
          "JUSTINQ",
          "JUSTINQ Inc."
        ]
      ]
    ]
    var filtered = rows.map(row=>row.filter(data=>data[1]!==''))
    console.log(filtered)
    Login or Signup to reply.
  4. You can use find to check if any empty string is found inside your array:

    const rows = 
    //    [
    //      ['ID', 'Name']
    //    ];
    
    [
      [
        [
          "11300273",
          "Domestic Supplier"
        ],
        [
          "11411110",
          "Plant Business Partner"
        ],
        [
          "11910008",
          ""
        ],
        [
          "54009145",
          "Slink"
        ],
        [
          "9980000000",
          ""
        ],
        [
          "9980000010",
          ""
        ],
        [
          "JUSTINQ",
          "JUSTINQ Inc."
        ]
      ]
    ]
    var filtered = rows[0].filter((entry) => {
      // Check if we can find any empty string
      let result = entry.find((value) => { return !value.length})
      // if we found it, we filter it out
      return typeof result !== 'string'
    }) 
    
    console.log(filtered)
    Login or Signup to reply.
  5. Based on what’s provided, try:

    const filtered = rows[0].filter(row => row[1] !== "");
    

    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…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search