使用下划线js的偶数或奇数

时间:2018-07-05 03:48:28

标签: underscore.js

var data =[{
    "_id" : 500,
    "bank_name" : "THE WEST BENGAL STATE COOPERATIVE BANK",
    "ifsc" : "WBSC0TUCB01",
    "micr" : "700093255",
    "branch_name" : "MAHINAGAR",
    "address" : "PO-MALANCHA-MAHINAGAR  VILL-MAHINAGAR",
    "contact" : "24379149",
    "city" : "KOLKATA",
    "district" : "24 PGS NORTH",
    "state" : "WEST BENGAL",
    "createdAt" :"2017-12-20T12:53:22.072",
    "updatedAt" :"2017-12-20T12:53:22.072"
},
{
    "_id" : 572,
    "bank_name" : "THE WEST BENGAL STATE COOPERATIVE BANK",
    "ifsc" : "WBSC0MUCB01",
    "micr" : null,
    "branch_name" : "KOLKATA MAHILA COOP BANK",
    "address" : "8D,KRISHNA LAHA LANE,BOWBAZAR",
    "contact" : "0",
    "city" : "KOLKATA",
    "district" : "KOLKATA",
    "state" : "WEST BENGAL",
    "createdAt" :"2017-12-20T12:53:22.082",
    "updatedAt" :"2017-12-20T12:53:22.082"
},
{
    "_id" : 587,
    "bank_name" : "THE WEST BENGAL STATE COOPERATIVE BANK",
    "ifsc" : "WBSC0MCCB18",
    "micr" : "742806512",
    "branch_name" : "MURSHIDABAD DIST CENTRAL COOP BANK",
    "address" : "VILL AND PO SAKTIPUR",
    "contact" : "251116",
    "city" : "KOLKATA",
    "district" : "MURSHIDABAD",
    "state" : "WEST BENGAL",
    "createdAt" :"2017-12-20T12:53:22.084",
    "updatedAt" :"2017-12-20T12:53:22.084"
},
{
    "_id" : 1,
    "bank_name" : "THE SURAT DISTRICT COOPERATIVE BANK LIMITED",
    "ifsc" : "SDCB0000099",
    "micr" : "395244002",
    "branch_name" : "MAIN BRANCH",
    "address" : "KANPITH, NEAR CHAUTAPOOL, PINCODE : 395003",
    "contact" : "2597730",
    "city" : "SURAT",
    "district" : "SURAT",
    "state" : "GUJARAT",
    "createdAt" :"2017-12-20T12:53:21.915",
    "updatedAt" :"2017-12-20T12:53:21.915"
},
{
    "_id" : 2,
    "bank_name" : "THE SURAT DISTRICT COOPERATIVE BANK LIMITED",
    "ifsc" : "SDCB0000085",
    "micr" : "395244039",
    "branch_name" : "ANAND MAHAL ROAD",
    "address" : "GROUND FLOOR, SHOP NO 1,2,3, MANISHA APARTMENT, NEAR SNEH SANKUL WADI, ANAND MAHAL ROAD, PIN - 395005",
    "contact" : "0",
    "city" : "SURAT",
    "district" : "SURAT",
    "state" : "GUJARAT",
    "createdAt" :"2017-12-20T12:53:21.929",
    "updatedAt" :"2017-12-20T12:53:21.929"
},
{
    "_id" : 4,
    "bank_name" : "THE SURAT DISTRICT COOPERATIVE BANK LIMITED",
    "ifsc" : "SDCB0000083",
    "micr" : "395244038",
    "branch_name" : "BHESTAN",
    "address" : "RAM NAGAR SOCIETY, SURAT-NAVSARI MAIN ROAD, BHESTAN, TA - CHORIYASI, PINCODE-395023",
    "contact" : "0",
    "city" : "SURAT",
    "district" : "SURAT",
    "state" : "GUJARAT",
    "createdAt" :"2017-12-20T12:53:21.931",
    "updatedAt" :"2017-12-20T12:53:21.931"
},
{
    "_id" : 5,
    "bank_name" : "THE SURAT DISTRICT COOPERATIVE BANK LIMITED",
    "ifsc" : "SDCB0000082",
    "micr" : "395244037",
    "branch_name" : "UDHANA",
    "address" : "THE UDHANA UDHYOG NAGAR SANGH LTD, SHOP NO 1,2,3,4 SEVEN SQUARE GROUND FLOOR, UDHANA, CHORIYASI, SURAT",
    "contact" : "0",
    "city" : "SURAT",
    "district" : "SURAT",
    "state" : "GUJARAT",
    "createdAt" :"2017-12-20T12:53:21.931",
    "updatedAt" :"2017-12-20T12:53:21.931"
},
{
    "_id" : 1,
    "bank_name" : "THE SURAT DISTRICT COOPERATIVE BANK LIMITED",
    "ifsc" : "SDCB0000099",
    "micr" : "395244002",
    "branch_name" : "MAIN BRANCH",
    "address" : "KANPITH, NEAR CHAUTAPOOL, PINCODE : 395003",
    "contact" : "2597730",
    "city" : "SURAT",
    "district" : "SURAT",
    "state" : "GUJARAT",
    "createdAt" :"2017-12-20T12:53:21.915",
    "updatedAt" :"2017-12-20T12:53:21.915"
},
{
    "_id" : 2,
    "bank_name" : "THE SURAT DISTRICT COOPERATIVE BANK LIMITED",
    "ifsc" : "SDCB0000085",
    "micr" : "395244039",
    "branch_name" : "ANAND MAHAL ROAD",
    "address" : "GROUND FLOOR, SHOP NO 1,2,3, MANISHA APARTMENT, NEAR SNEH SANKUL WADI, ANAND MAHAL ROAD, PIN - 395005",
    "contact" : "0",
    "city" : "SURAT",
    "district" : "SURAT",
    "state" : "GUJARAT",
    "createdAt" :"2017-12-20T12:53:21.929",
    "updatedAt" :"2017-12-20T12:53:21.929"
},
{
    "_id" : 3,
    "bank_name" : "THE SURAT DISTRICT COOPERATIVE BANK LIMITED",
    "ifsc" : "SDCB0000084",
    "micr" : "NA",
    "branch_name" : "KAPURA",
    "address" : "HOUSE NO - 135, AT AND POST - KAPURA, TALUKA - VYARA, PIN - 394655",
    "contact" : "0",
    "city" : "VYARA",
    "district" : "TAPI",
    "state" : "GUJARAT",
    "createdAt" :"2017-12-20T12:53:21.931",
    "updatedAt" :"2017-12-20T12:53:21.931"
},
{
    "_id" : 4,
    "bank_name" : "THE SURAT DISTRICT COOPERATIVE BANK LIMITED",
    "ifsc" : "SDCB0000083",
    "micr" : "395244038",
    "branch_name" : "BHESTAN",
    "address" : "RAM NAGAR SOCIETY, SURAT-NAVSARI MAIN ROAD, BHESTAN, TA - CHORIYASI, PINCODE-395023",
    "contact" : "0",
    "city" : "SURAT",
    "district" : "SURAT",
    "state" : "GUJARAT",
    "createdAt" :"2017-12-20T12:53:21.931",
    "updatedAt" :"2017-12-20T12:53:21.931"
}];

这是我的数据。我想找到所有数组的索引,并从中找到偶数或奇数索引。.

2 个答案:

答案 0 :(得分:1)

您可以使用以下代码:

 var data [.... From Question ....];

 var allIndex = _.keys(data) 
 var evens = _.filter(allIndex, function(num){ return num % 2 == 0; });
 var olds = _.filter(allIndex, function(num){ return num % 2 != 0; });

输出:

 allIndex: [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" ]

 evens: [ "0", "2", "4", "6", "8", "10" ]

 olds: [ "1", "3", "5", "7", "9" ]

求和

 var sum = 0;
 _.each(allIndex, function (item) {
    sum += parseInt(item);
 });

输出:

 sum: 55

如果不使用parseInt()方法,它将连接值

jsfiddle

答案 1 :(得分:1)

var data = [{
    "_id": 500,
    "bank_name": "THE WEST BENGAL STATE COOPERATIVE BANK",
    "ifsc": "WBSC0TUCB01",
    "micr": "700093255",
    "branch_name": "MAHINAGAR",
    "address": "PO-MALANCHA-MAHINAGAR  VILL-MAHINAGAR",
    "contact": "24379149",
    "city": "KOLKATA",
    "district": "24 PGS NORTH",
    "state": "WEST BENGAL",
    "createdAt": "2017-12-20T12:53:22.072",
    "updatedAt": "2017-12-20T12:53:22.072"
  },
  {
    "_id": 572,
    "bank_name": "THE WEST BENGAL STATE COOPERATIVE BANK",
    "ifsc": "WBSC0MUCB01",
    "micr": null,
    "branch_name": "KOLKATA MAHILA COOP BANK",
    "address": "8D,KRISHNA LAHA LANE,BOWBAZAR",
    "contact": "0",
    "city": "KOLKATA",
    "district": "KOLKATA",
    "state": "WEST BENGAL",
    "createdAt": "2017-12-20T12:53:22.082",
    "updatedAt": "2017-12-20T12:53:22.082"
  },
  {
    "_id": 587,
    "bank_name": "THE WEST BENGAL STATE COOPERATIVE BANK",
    "ifsc": "WBSC0MCCB18",
    "micr": "742806512",
    "branch_name": "MURSHIDABAD DIST CENTRAL COOP BANK",
    "address": "VILL AND PO SAKTIPUR",
    "contact": "251116",
    "city": "KOLKATA",
    "district": "MURSHIDABAD",
    "state": "WEST BENGAL",
    "createdAt": "2017-12-20T12:53:22.084",
    "updatedAt": "2017-12-20T12:53:22.084"
  },
  {
    "_id": 1,
    "bank_name": "THE SURAT DISTRICT COOPERATIVE BANK LIMITED",
    "ifsc": "SDCB0000099",
    "micr": "395244002",
    "branch_name": "MAIN BRANCH",
    "address": "KANPITH, NEAR CHAUTAPOOL, PINCODE : 395003",
    "contact": "2597730",
    "city": "SURAT",
    "district": "SURAT",
    "state": "GUJARAT",
    "createdAt": "2017-12-20T12:53:21.915",
    "updatedAt": "2017-12-20T12:53:21.915"
  },
  {
    "_id": 2,
    "bank_name": "THE SURAT DISTRICT COOPERATIVE BANK LIMITED",
    "ifsc": "SDCB0000085",
    "micr": "395244039",
    "branch_name": "ANAND MAHAL ROAD",
    "address": "GROUND FLOOR, SHOP NO 1,2,3, MANISHA APARTMENT, NEAR SNEH SANKUL WADI, ANAND MAHAL ROAD, PIN - 395005",
    "contact": "0",
    "city": "SURAT",
    "district": "SURAT",
    "state": "GUJARAT",
    "createdAt": "2017-12-20T12:53:21.929",
    "updatedAt": "2017-12-20T12:53:21.929"
  },
  {
    "_id": 4,
    "bank_name": "THE SURAT DISTRICT COOPERATIVE BANK LIMITED",
    "ifsc": "SDCB0000083",
    "micr": "395244038",
    "branch_name": "BHESTAN",
    "address": "RAM NAGAR SOCIETY, SURAT-NAVSARI MAIN ROAD, BHESTAN, TA - CHORIYASI, PINCODE-395023",
    "contact": "0",
    "city": "SURAT",
    "district": "SURAT",
    "state": "GUJARAT",
    "createdAt": "2017-12-20T12:53:21.931",
    "updatedAt": "2017-12-20T12:53:21.931"
  },
  {
    "_id": 5,
    "bank_name": "THE SURAT DISTRICT COOPERATIVE BANK LIMITED",
    "ifsc": "SDCB0000082",
    "micr": "395244037",
    "branch_name": "UDHANA",
    "address": "THE UDHANA UDHYOG NAGAR SANGH LTD, SHOP NO 1,2,3,4 SEVEN SQUARE GROUND FLOOR, UDHANA, CHORIYASI, SURAT",
    "contact": "0",
    "city": "SURAT",
    "district": "SURAT",
    "state": "GUJARAT",
    "createdAt": "2017-12-20T12:53:21.931",
    "updatedAt": "2017-12-20T12:53:21.931"
  },
  {
    "_id": 1,
    "bank_name": "THE SURAT DISTRICT COOPERATIVE BANK LIMITED",
    "ifsc": "SDCB0000099",
    "micr": "395244002",
    "branch_name": "MAIN BRANCH",
    "address": "KANPITH, NEAR CHAUTAPOOL, PINCODE : 395003",
    "contact": "2597730",
    "city": "SURAT",
    "district": "SURAT",
    "state": "GUJARAT",
    "createdAt": "2017-12-20T12:53:21.915",
    "updatedAt": "2017-12-20T12:53:21.915"
  },
  {
    "_id": 2,
    "bank_name": "THE SURAT DISTRICT COOPERATIVE BANK LIMITED",
    "ifsc": "SDCB0000085",
    "micr": "395244039",
    "branch_name": "ANAND MAHAL ROAD",
    "address": "GROUND FLOOR, SHOP NO 1,2,3, MANISHA APARTMENT, NEAR SNEH SANKUL WADI, ANAND MAHAL ROAD, PIN - 395005",
    "contact": "0",
    "city": "SURAT",
    "district": "SURAT",
    "state": "GUJARAT",
    "createdAt": "2017-12-20T12:53:21.929",
    "updatedAt": "2017-12-20T12:53:21.929"
  },
  {
    "_id": 3,
    "bank_name": "THE SURAT DISTRICT COOPERATIVE BANK LIMITED",
    "ifsc": "SDCB0000084",
    "micr": "NA",
    "branch_name": "KAPURA",
    "address": "HOUSE NO - 135, AT AND POST - KAPURA, TALUKA - VYARA, PIN - 394655",
    "contact": "0",
    "city": "VYARA",
    "district": "TAPI",
    "state": "GUJARAT",
    "createdAt": "2017-12-20T12:53:21.931",
    "updatedAt": "2017-12-20T12:53:21.931"
  },
  {
    "_id": 4,
    "bank_name": "THE SURAT DISTRICT COOPERATIVE BANK LIMITED",
    "ifsc": "SDCB0000083",
    "micr": "395244038",
    "branch_name": "BHESTAN",
    "address": "RAM NAGAR SOCIETY, SURAT-NAVSARI MAIN ROAD, BHESTAN, TA - CHORIYASI, PINCODE-395023",
    "contact": "0",
    "city": "SURAT",
    "district": "SURAT",
    "state": "GUJARAT",
    "createdAt": "2017-12-20T12:53:21.931",
    "updatedAt": "2017-12-20T12:53:21.931"
  }
];

function test() {
  var indexs = _.keys(data)
  var evenIndex = _.filter(indexs, function(num) {
    return num % 2 == 0;
  });
  var oldIndex = _.filter(indexs, function(num) {
    return num % 2 != 0;
  });

  var sum = 0;
  _.each(indexs, function(item) {
    sum += parseInt(item);
  });

  log("Indexs", indexs);
  log("Even Indexs", evenIndex);
  log("Old Indexs", oldIndex);
  log("Sum", sum);
}

// log func
function log(name, msg) {
  if (_.isUndefined(msg)) {
    msg = name;
    name = null;
  }
  if (_.isObject(msg)) {
    msg = JSON.stringify(msg, null, 2);
  }
  msg = (name ? name + ': ' : '') + msg;
  $('#content').append($('<p></p>').html(msg));
}

test();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>

<div id="content">

</div>