简体   繁体   English

Javascript数组过滤器函数删除键值

[英]Javascript array filter function removes key values

I have an array of Javascript objects indexed by key values listed below, each key representing a Javascript object (this is just a console.log() of the key values in the array):我有一个由下面列出的键值索引的 Javascript 对象数组,每个键代表一个 Javascript 对象(这只是数组中键值的 console.log() ):

[ '532',
  '789',
  '1232',
  '2346',
  '3404',
  '4991',
  '5323',
  '5378',
  '8923',
  '9876',
  '23434',
  '23549',
  '24454',
  '34234',
  '45667',
  '48983',
  '67834',
  '72342',
  '82434',
  '89829',
  '98732',
  '123404',
  '143454',
  '234345',
  '345294',
  '532234',
  '532342',
  '532345',
  '532349',
  '989898' ]

When I console.log() out this Javascript array it is "filled-in" with "NULL" values.当我 console.log() 输出这个 Javascript 数组时,它用“NULL”值“填充”。 For example the first key is 532, so when printing this array out first there are 531 "NULL" values printed and the then Javascript object with key 532 and so on for each key.例如,第一个键是 532,所以当首先打印这个数组时,会打印 531 个“NULL”值,然后是带有键 532 的 Javascript 对象,每个键依此类推。

So my solution is to remove the null values by running this function below on the array:所以我的解决方案是通过在数组上运行下面的这个函数来删除空值:

teamData = teamData.filter(function (el) {
   return el != null && el != "";
});

My Issue: .我的问题: Once I run that filter function the array key values are reset to:运行该过滤器函数后,数组键值将重置为:

[ '0',
  '1',
  '2',
  '3',
  '4',
  '5',
  '6',
  '7',
  '8',
  '9',
  '10',
  '11',
  '12',
  '13',
  '14',
  '15',
  '16',
  '17',
  '18',
  '19',
  '20',
  '21',
  '22',
  '23',
  '24',
  '25',
  '26',
  '27',
  '28',
  '29' ]

Question: Is there a way to filter out the null values between the array keys while still keeping the key values in the first array?问题:有没有办法过滤掉数组键之间的空值,同时仍然保留第一个数组中的键值? Is there another way to structure this data so there won't be null values?有没有另一种方法来构造这个数据,所以不会有空值?

Problem is that you're trying to use an array index position as key.问题是您试图使用数组索引位置作为键。 This won't work because arrays always have consecutive indexes starting from 0. So if you gonna have an element at index 532 then there necessarily will be 532 elements before it (0 to 531).这是行不通的,因为数组总是有从 0 开始的连续索引。所以如果你要在索引 532 处有一个元素,那么它之前一定会有 532 个元素(0 到 531)。 If you remove those elements with filter the indexes will shift to the correct position, in your case from 0 o 29. In Javascript there is a native object for your use case, it is called Map, here you can read everything about it.如果您使用filter删除这些元素,则索引将移动到正确的位置,在您的情况下是从 0 到 29。在 Javascript 中,您的用例有一个本机对象,它称为 Map, 在这里您可以阅读有关它的所有内容。

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

相关问题 JavaScript数组过滤器功能仅删除带有拼接的一个元素 - JavaScript Array filter function removes only one element with splice JavaScript 根据键值数组从列表中删除对象 - JavaScript removes objects from list based on an array of key values 从值数组 JavaScript 中过滤键上的对象数组 - Filter array of objects on key from array of values JavaScript 使用一个键的值数组过滤许多javascript对象 - Filter a number of javascript objects using an array of values for one key 如何在JavaScript过滤器函数中访问键值对中的数组 - How to access an array in key-value pair in a javascript filter function 通过传递数组值,使用过滤器 function 循环 JSON 字符串,Javascript - Loop the JSON String using filter function by passing array values, Javascript Javascript中过滤多个数组对象,匹配键值与多个值形成另一个数组 - Filter multiple Array Objects and match key values with multiple values form another array in Javascript Javascript Minifier删除功能 - Javascript minifier removes function Function 仅从数组中删除项目但不更改值 - Function only removes items from array but does not change values 需要帮助了解删除数组中的重复项并求和值的功能 - Need Help Understanding Function that Removes Duplicates in Array and Sums up values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM