Home > Net >  Remove numeric values from an object where the values are all strings but a mix of letter and number
Remove numeric values from an object where the values are all strings but a mix of letter and number

Time:12-02

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?

CodePudding user response:

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.

CodePudding user response:

We define a list of supervisors, each with a jurisdiction, last name, and first name.

We create a new array of supervisors with integer values removed from the jurisdiction. This is done by filtering the original array of supervisors and only including elements whose jurisdiction property does not contain an integer.

We sort the array of supervisors alphabetically by jurisdiction. This is done by using the Array.prototype.sort() method and providing a comparison function that compares the jurisdiction property of each element.

We print the resulting array of supervisors by using the Array.prototype.forEach() method and a callback function that prints each supervisor.

Here is the complete code again for reference:

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

// Create a new array of supervisors with integer values removed from the jurisdiction
const filteredSupervisors = supervisors.filter(
    supervisor => !Number.isInteger(Number(supervisor.jurisdiction))
);

// Sort the array of supervisors alphabetically by jurisdiction
const sortedSupervisors = filteredSupervisors.sort(
    (a, b) => a.jurisdiction.localeCompare(b.jurisdiction)
);

// Print the resulting array of supervisors
sortedSupervisors.forEach(supervisor => console.log(supervisor));

CodePudding user response:

The following snippet will first filter out all numeric .jurisdiction entries and will sort the remaining entries in a case-insensitive manner according to jurisdiction, lastName, firstName:

const supervisors= [
{
    "jurisdiction": "u",
    "lastName": "Olson",
    "firstName": "Karson"
},
{
    "jurisdiction": "9",
    "lastName": "Heller",
    "firstName": "Robbie"
},
{
    "jurisdiction": "b",
    "lastName": "Cremin",
    "firstName": "Elijah"
},
{
    "jurisdiction": "b",
    "lastName": "Cremmin",
    "firstName": "Elijah"
},
{
    "jurisdiction": "b",
    "lastName": "Cremin",
    "firstName": "Daniel"
},
{
    "jurisdiction": "b",
    "lastName": "Cremin",
    "firstName": "eddie"
}
];

const res=supervisors.filter(s=>isNaN(parseFloat(s.jurisdiction)))
  .sort((a,b)=>{
      let ab=0;
      ["jurisdiction","lastName","firstName"].some(p=>ab=a[p].toLowerCase().localeCompare(b[p].toLowerCase()));
      return ab;
});

console.log(res);

The actual comparisons take place in the inner .some() loop. The loop will stop processing as soon as it generates a result ab that is not equal to zero.

  • Related