使用javascript从数组中打印x个x对象

时间:2017-04-20 20:20:57

标签: javascript jquery arrays sorting

使用javascript对对象数组进行排序或查询的最佳方法是什么?说我想只获得前两个对象,然后是接下来的两个,或者从第5个开始获得5个?

我应该看一下哪些功能?

var obj = [
  {
    "_id": "58f8ffe7ce2bbf01c6164801",
    "isActive": true
  },
    {
    "_id": "58f8ffe7ce2bbf01c6164802",
    "isActive": true
  },
    {
    "_id": "58f8ffe7ce2bbf01c6164803",
    "isActive": true
  },
    {
    "_id": "58f8ffe7ce2bbf01c6164804",
    "isActive": true
  },
    {
    "_id": "58f8ffe7ce2bbf01c6164805",
    "isActive": true
  },
    {
    "_id": "58f8ffe7ce2bbf01c6164806",
    "isActive": true
  },
    {
    "_id": "58f8ffe7ce2bbf01c6164807",
    "isActive": true
  },
    {
    "_id": "58f8ffe7ce2bbf01c6164808",
    "isActive": true
  },
    {
    "_id": "58f8ffe7ce2bbf01c6164809",
    "isActive": true
  },
    {
    "_id": "58f8ffe7ce2bbf01c6164810",
    "isActive": true
  }
]

1 个答案:

答案 0 :(得分:3)

obj.slice(0,2);//first two
obj.slice(2,4);//next two

休息时根据切片API进行数学运算。