javascript - 如何处理和检查唯一ID?

标签 javascript arrays transactions rows indexof

我有多个交易行,我想看看是否可以在 for 循环中处理每个交易行,或者至少将它们存储在一个数组中,以便我可以查找并检查交易 ID 是否已被引用。

我的数据如下:

PersonID | TranID | LineID | Info
123      |  1     | 0      | Apples
123      |  1     | 1      | Oranges
145      |  2     | 0      | Bananas
145      |  2     | 1      | Popcorn
145      |  2     | 2      | Mushrooms

以及我想要独特解析的内容:

123
#1
Apples
Oranges

~~

145
#2
Bananas
Popcorn
Mushrooms

我在想:

var transaction = new Array();

for (var i = 0; datarows != null && i <= datarows.length; i++){

   if datarows[i].tranID is not in transaction{

   write the rows
   transaction.push(datarows[i].tranID)

   } else {
   return;
 }
}

问题是..我实际上并不存储线路ID,所以我无法处理每一行。救命!

最佳答案

好的..这是你需要做的(假设你正在使用 jQuery - 我通常发现它更容易)-

var dataArray = [
  {personId: 123, tranId: 1, lineId: 0, info: 'Apples'},
  {personId:123, tranId:1, lineId:1, info:'Oranges'},
  {personId:145, tranId:2, lineId:0, info:'Bananas'},
  {personId:145, tranId:2, lineId:1, info:'Popcorn'},
  {personId:145, tranId:2, lineId:2, info:'Mushrooms'}
];

//Don't bother about this
$.each(dataArray, function(colIndex, item){ 
  
  var row = $('<tr/>'); 
  
  $.each(item, function(rowIndex, it){       
    row.append($('<td/>').text(it));    
  });
    
  $('#data tbody').append(row);
});

// Have an array for filtered objects
var filteredArray = [];

// Iterate through data array
$.each(dataArray, function(index, item){
  
  if(filteredArray.length == 0)
    filteredArray.push(item);
  
  // a flag to tell you if the data exists in the filtered array or not
  var addItem = true;
  
  // Check if the data exists in the filtered array
  var filteredItem = $.grep(filteredArray, function(filItem, filIndex, filAll){
    
    // Here is where you check whatever conditions you want
    if(filItem.personId == item.personId && filItem.tranId == item.tranId) {
     addItem = false;
      return false;
    }      
  });
  
  // Add the data
  if(addItem)
    filteredArray.push(item);
});


//Don't bother about this
$.each(filteredArray, function(colIndex, item){ 
  
  var row = $('<tr/>'); 
  
  $.each(item, function(rowIndex, it){       
    row.append($('<td/>').text(it));    
  });
    
  $('#filteredData tbody').append(row);
});
#data{
  border: grey 1px solid;
}

#filteredData{
  border: grey 1px solid;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="data">
  <thead>
  <th>PersonID</th>
  <th>TranID</th>
  <th>LineID</th>
  <th>Info</th>  
  </thead>
  <tbody></tbody>
</table>
<br/><br/>

<table id="filteredData">
   <thead>
  <th>PersonID</th>
  <th>TranID</th>
  <th>LineID</th>
  <th>Info</th>  
  </thead>
  <tbody></tbody>
</table>

此外,这是 CodePen - http://codepen.io/aswinramakrish/pen/YPQZBj

关于javascript - 如何处理和检查唯一ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28118110/

相关文章:

php - 具有多种变体的 Foreach 字符串替换

mysql - SQL 事务 ELSE IF 和 SELECT

javascript - 为什么我的 Google map API v3 和侧面板在调整大小时无法填满我的页面?

php - Jquery/PHP if 语句拖放更新数组并插入到 MySQL 表中?

javascript - 更优雅的数组二次排序

python - Django 1.6 事务以避免竞争条件

hibernate - 嵌套@Transactional

javascript - 始终触发且仅触发一次的 "transitionend"事件

javascript - 使用 JQuery 在 ESC 按键处关闭自定义框

javascript - 为什么 Array.prototype.join() 会展平任意深度的数组?