1

I'm sending different objects to a component outside, and component data varies depending on objects. I'm getting the names with the Object.key function because the keywords I send have different key. Then I want to sort by the key. For this I need to define the name I received with Object.key function. How can I do it?

 upSortTable(items, val) {
  //items = Object,
  //val = index
  let Keys = Object.keys(items[0]); // ["item_id","item_title"]
  let keyname = Keys[val]; //item_id String value
  //want to use in sort function as b.item_id
  return items.sort(function(a, b) {
    return b.keyname - a.keyname;
  });
},

1 Answer 1

3

You'll need to use computed property:

return items.sort(function(a, b) {
  return b[keyname] - a[keyname];
});

When you do a.keyname you're actually looking for the property keyname in a itself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.