jquery - Sorting associative array of objects in javascript - Stack Overflow

I have a particular array of objects in js that represent columns of a table.My object is:var columns

I have a particular array of objects in js that represent columns of a table. My object is:

var columns = {
  "image": {
    "show": false,
    "orderable": true,
    "value": 0,
    "displayOrder":2
  },
  "name": {
    "show": true,
    "orderable": true,
    "value": 1,
    "displayOrder":0
  },
  "pany": {
    "show": false,
    "orderable": false,
    "value": 2,
    "displayOrder":1
  }
}

I have to order the object by "displayOrder", but using a function like

columns.sort(function(a, b) {
  return parseFloat(a.displayOrder) - parseFloat(b.displayOrder); 
});

obviously it return an error. How i can do this?

I have a particular array of objects in js that represent columns of a table. My object is:

var columns = {
  "image": {
    "show": false,
    "orderable": true,
    "value": 0,
    "displayOrder":2
  },
  "name": {
    "show": true,
    "orderable": true,
    "value": 1,
    "displayOrder":0
  },
  "pany": {
    "show": false,
    "orderable": false,
    "value": 2,
    "displayOrder":1
  }
}

I have to order the object by "displayOrder", but using a function like

columns.sort(function(a, b) {
  return parseFloat(a.displayOrder) - parseFloat(b.displayOrder); 
});

obviously it return an error. How i can do this?

Share Improve this question edited Oct 20, 2017 at 14:04 dhilt 20.9k8 gold badges79 silver badges93 bronze badges asked Oct 20, 2017 at 12:39 RubeRube 1053 silver badges11 bronze badges 9
  • You have an object, and the properties of an object cannot be sorted. If you want to do this, columns needs to be an array. – Rory McCrossan Commented Oct 20, 2017 at 12:41
  • Why you do parseFloat on integer? Better use parseInt then. – kevinSpaceyIsKeyserSöze Commented Oct 20, 2017 at 12:42
  • Its not possible to sort an object, but you could create an arrayofobjects like shown here stackoverflow./questions/1069666/… – Doomenik Commented Oct 20, 2017 at 12:43
  • @kevinSpaceyIsKeyserSöze just copy-paste problem. – Rube Commented Oct 20, 2017 at 12:46
  • 1 I don't know what that's supposed to mean. JS has no equivalent of associative arrays. There are objects (ie. what you have in the code above) and arrays. The properties of objects are transient, and cannot be sorted. – Rory McCrossan Commented Oct 20, 2017 at 12:51
 |  Show 4 more ments

1 Answer 1

Reset to default 7

You may use .sort only on arrays due to it is Array.prototype method (MDN). So as an easiest solution I would prefer to reform your columns data from object to array:

var columnList = [
    {
      "key": "image",
      "value": {
          "show": false,
          "orderable": true,
          "value": 0,
          "displayOrder":2
      }
    },
    {
      "key": "name",
      "value": {
          "show": true,
          "orderable": true,
          "value": 1,
          "displayOrder":0
      } 
    },
    {
      "key": "pany",
      "value": {
          "show": false,
          "orderable": false,
          "value": 2,
          "displayOrder":1
      } 
    }
];

var result = columnList.sort(function(a, b) {
    return parseFloat(a.value.displayOrder) - parseFloat(b.value.displayOrder);  
});

console.log(result);

The result of console.log would be exactly

0:{key: "name", value: {…}}
1:{key: "pany", value: {…}}
2:{key: "image", value: {…}}

This transformation (from object to array) could be done programmatically via Object.keys:

result = Object.keys(columns)
  .map(c => ({ key: c, value: columns[c]}))
  .sort((a, b) => a.value.displayOrder - b.value.displayOrder)

Here the columns is your initial object. With the help of Object.keys() and .map() I turn it into the array I described before, so the .sort() method could be called in a chain.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742305970a4418947.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信