How to put a few functions in one?

Hi,

I created user list based on list.js library. Everything is done, but I wanted to make shorter my code in script.js;

Here is my code:

var options = {
    valueNames: ['ordinal_number', 'firstname', 'lastname', 'phone_number', 'age', 'city']
};

var listObj = new List('users', options)

function numb_up() {
    listObj.sort('ordinal_number', { order: "asc" });
}

function numb_down() {
    listObj.sort('ordinal_number', { order: "desc" });
}

function name_down() {
    listObj.sort('firstname', { order: "asc" });
}

function name_up() {
    listObj.sort('firstname', { order: "desc" });
}

function last_down() {
    listObj.sort('lastname', { order: "asc" });
}

function last_up() {
    listObj.sort('lastname', { order: "desc" });
}

function phone_down() {
    listObj.sort('phone_number', { order: "asc" });
}

function phone_up() {
    listObj.sort('phone_number', { order: "desc" });
}

function age_down() {
    listObj.sort('age', { order: "asc" });
}

function age_up() {
    listObj.sort('age', { order: "desc" });
}


function city_down() {
    listObj.sort('city', { order: "asc" });
}

function city_up() {
    listObj.sort('city', { order: "desc" });
}

Thank you in advance,
Megi

Shorter like this?

var options = {
    valueNames: ["ordinal_number", "firstname", "lastname", "phone_number", "age", "city"]
};
var listObj = new List("users", options);
const numb_up = () => listObj.sort("ordinal_number", {order: "asc"});
const numb_down = () => listObj.sort("ordinal_number", {order: "desc"});
const name_down = () => listObj.sort("firstname", {order: "asc"});
const name_up = () => listObj.sort("firstname", {order: "desc"});
const last_down = () => listObj.sort("lastname", {order: "asc"});
const last_up = () => listObj.sort("lastname", {order: "desc"});
const phone_down = () => listObj.sort("phone_number", {order: "asc"});
const phone_up = () => listObj.sort("phone_number", {order: "desc"});
const age_down = () => listObj.sort("age", {order: "asc"});
const age_up = () => listObj.sort("age", {order: "desc"});
const city_down = () => listObj.sort("city", {order: "asc"});
const city_up = () => listObj.sort("city", {order: "desc"});

Or shorter, like this:

var options={valueNames:[‘ordinal_number’,‘firstname’,‘lastname’,‘phone_number’,‘age’,‘city’]},listObj=new List(‘users’,options);function numb_up(){listObj.sort(‘ordinal_number’,{order:‘asc’})}function numb_down(){listObj.sort(‘ordinal_number’,{order:‘desc’})}function name_down(){listObj.sort(‘firstname’,{order:‘asc’})}function name_up(){listObj.sort(‘firstname’,{order:‘desc’})}function last_down(){listObj.sort(‘lastname’,{order:‘asc’})}function last_up(){listObj.sort(‘lastname’,{order:‘desc’})}function phone_down(){listObj.sort(‘phone_number’,{order:‘asc’})}function phone_up(){listObj.sort(‘phone_number’,{order:‘desc’})}function age_down(){listObj.sort(‘age’,{order:‘asc’})}function age_up(){listObj.sort(‘age’,{order:‘desc’})}function city_down(){listObj.sort(‘city’,{order:‘asc’})}function city_up(){listObj.sort(‘city’,{order:‘desc’})}

Thank you, I really like the solutions with const.

You might like the following update then:

const userFields = {
    numb: "ordinal_number",
    name: "firstname",
    last: "lastname",
    phone: "phone_number",
    age: "age",
    city: "city"
};
const listObj = new List("users", {
    valueNames: Object.values(userFields)
});

function sortListUp(name) {
    listObj.sort(userFields[name], {order: "asc"});
}
function sortListDown(name) {
    listObj.sort(userFields[name], {order: "desc"});
}

Where you can use sortListUp("last") to sort the list by lastname in ascending order

As a further refinement, default to sorting in ascending direction, but allow “asc” or “desc” to be used too.

const userFields = {
    numb: "ordinal_number",
    name: "firstname",
    last: "lastname",
    phone: "phone_number",
    age: "age",
    city: "city"
};
const listObj = new List("users", {
    valueNames: Object.values(userFields)
});

function sortList(name, dir = "asc") {
    listObj.sort(userFields[name], {order: dir});
}

Where you can use, for example:

  • sortList("name")
  • sortList("age", "desc")
  • sortList("city", "asc")

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.