简体   繁体   English

将值作为单独的键值对推送到数组

[英]Push values to array as separate key value pairs

I have a project where I'm adding values from an attribute (comma separated integers) on a particular cell in each row of a table to an array in JS.我有一个项目,我将表格每一行中特定单元格上的属性(逗号分隔的整数)的值添加到 JS 中的数组中。

I know that if I create an array called myArray, then use myArray.push(121840,121841);我知道如果我创建一个名为 myArray 的数组,则使用myArray.push(121840,121841); the myArray.length result would be 2. This is what I expected. myArray.length结果为 2。这是我的预期。 I had assumed (incorrectly) that since the value of the numbers attribute was the same format, eg: numbers="121840,121841" , then the result would be the same using myArray.push($(this).attr('numbers'));我曾假设(错误地)由于 numbers 属性的值是相同的格式,例如: numbers="121840,121841" ,那么使用myArray.push($(this).attr('numbers')); , but I was mistaken as the length of that array is 1, instead of 2. ,但我被误认为该数组的长度是 1,而不是 2。

See below an example of what I'm trying to do and the issue I'm encountering.请参阅下面的示例,说明我正在尝试做什么以及遇到的问题。

Given a table like this where I'm grabbing the values from the last cell's numbers attribute:给定这样的表格,我从最后一个单元格的 numbers 属性中获取值:

<table border="1" width="100%">
    <tbody emp-id="02" class="" name="Steve Smith">
      <tr>
          <td colspan="4">Steve Smith</td>
      </tr>
      <tr>
          <td></td>
          <td>2</td>
          <td></td>
          <td class="total" numbers="121856,121860">2</td>
      </tr>
    </tbody>
    <tbody emp-id="01" name="Marky Mark">
      <tr>
          <td colspan="4">Marky Mark</td>
      </tr>
      <tr>
          <td>1</td>
          <td></td>
          <td>1</td>
          <td class="total" numbers="121840,121841">2</td>
      </tr>
    </tbody>
</table>

My JS would is:我的 JS 会是:

$('table tbody tr').each(function() {
  $(this).find('td:last').each(function(){
    if ($(this).attr('numbers')) {
        numbers.push($(this).attr('numbers'));
      names.push($(this).parents("tbody").attr('name'));
    }
  });
});

In the above example, the array has the correct number values stored, (121856,121860,121840,12184), but the length is given as 2 as each cell's values was added as a single element, such that number[0]=121856,121860, instead of 121856.在上面的示例中,数组存储了正确的数值 (121856,121860,121840,12184),但长度为 2,因为每个单元格的值作为单个元素添加,因此 number[0]=121856 ,121860,而不是 121856。

How would I correct this so that each integer within the attribute is added as a single element?我将如何更正此问题,以便将属性中的每个整数添加为单个元素?

JSFiddle: http://jsfiddle.net/jacbhg0n/3/ JSFiddle:http: //jsfiddle.net/jacbhg0n/3/

Any help would be greatly appreciated.任何帮助将不胜感激。

You can simply achieve that by splitting the numbers attribute string by using String.split() method while pushing it into the numbers array.您可以通过使用String.split()方法拆分数字属性字符串,同时将其推入numbers数组来简单地实现这一点。

Live Demo :现场演示

 const numbers = []; const names = []; $('table tbody tr').each(function() { $(this).find('td:last').each(function(){ if ($(this).attr('numbers')) { numbers.push($(this).attr('numbers').split(',')); names.push($(this).parents("tbody").attr('name')); } }); }); console.log(numbers.flat());
 <table border="1" width="100%"> <tbody emp-id="02" class="" name="Steve Smith"> <tr> <td colspan="4">Steve Smith</td> </tr> <tr> <td></td> <td>2</td> <td></td> <td class="total" numbers="121856,121860">2</td> </tr> </tbody> <tbody emp-id="01" name="Marky Mark"> <tr> <td colspan="4">Marky Mark</td> </tr> <tr> <td>1</td> <td></td> <td>1</td> <td class="total" numbers="121840,121841">2</td> </tr> </tbody> </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM