【问题标题】:Remove numeric values from an object where the values are all strings but a mix of letter and numbersRemove numeric values from an object where the values are all strings but a mix of letter and numbers
【发布时间】:2022-12-02 07:55:13
【问题描述】:

I have to call an api that returns an array of objects:

"supervisors": [
    {
        "jurisdiction": "u",
        "lastName": "Olson",
        "firstName": "Karson"
    },
    {
        "jurisdiction": "9",
        "lastName": "Heller",
        "firstName": "Robbie"
    },
    {
        "jurisdiction": "b",
        "lastName": "Cremin",
        "firstName": "Elijah"
    },
]

The supervisors must be sorted in alphabetical order, first by jurisdiction, then my last name, finally by first name. Then Numeric jurisdictions should be removed from the response.

I sorted alphabetically by:

supervisorsObj.sort((a, b) => {
      a.jurisdiction.toLowerCase().localeCompare(b.jurisdiction.toLowerCase());
    });

But how do I remove Numeric jurisdictions if they are all strings?

【问题讨论】:

    标签: javascript sorting object


    【解决方案1】:

    One option would be to use regular expressions, e.g

    filtered = data.supervisors.filter(s => !/^d+$/.test(s.jurisdiction))
    

    will only remove jurisdictions that consists entirely of digits.

    【讨论】:

      猜你喜欢
      • 2022-12-02
      • 2022-12-02
      • 2022-12-01
      • 2022-12-02
      • 2022-12-02
      • 2017-03-22
      • 1970-01-01
      • 2022-12-01
      • 2022-12-02
      相关资源
      最近更新 更多