简体   繁体   English

javascript 中包含空值的完整数组

[英]Complete array with nulls in javascript

This is a similar question as this one: Complete missing sequence in array with zeros in javascript这是一个与此类似的问题: Complete missing sequence in array with zeros in javascript

However, I can't seem to go around this problem.但是,我似乎无法解决这个问题。 This is my array:这是我的数组:

 const array = [ [5, 'a', 2.3], [6, 'a', 1.7], [7, 'a', 5.4], [8, 'a', 2.8], [9, 'a', 8.5], [10, 'a', 9.2], [2, 'b', 1.6], [5, 'b', 5.7], [6, 'b', 8.9], [7, 'b', 3.5], [8, 'b', 6.1], [9, 'b', 1.8], [10, 'b', 7.4], ]; console.log(array);

First element : this is my reference value, it ranges from 1 to 10.第一个元素:这是我的参考值,范围从 1 到 10。

Second element : this is a category value.第二个元素:这是一个类别值。

Third element : this is a value that belongs to the second element, which happened at a timestamp that belongs to the first element.第三个元素:这是属于第二个元素的值,它发生在属于第一个元素的时间戳。


My issue: I need to make sure that all the unique categories in the second element of the array (eg, a and b ) have the following sequence in the first element: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] .我的问题:我需要确保数组的第二个元素(例如ab )中的所有唯一类别在第一个元素中具有以下顺序: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] If they do not have one of these numbers, then I need to create it, and then assign null to the third element.如果他们没有这些数字之一,那么我需要创建它,然后将null分配给第三个元素。

Therefore, this is my expected output :因此,这是我预期的 output

[
  [1, 'a', null],
  [2, 'a', null],
  [3, 'a', null],
  [4, 'a', null],
  [5, 'a', 2.3],
  [6, 'a', 1.7],
  [7, 'a', 5.4],
  [8, 'a', 2.8],
  [9, 'a', 8.5],
  [10, 'a', 9.2],
  [1, 'b', null],
  [2, 'b', 1.6],
  [3, 'b', null],
  [4, 'b', null],
  [5, 'b', 5.7],
  [6, 'b', 8.9],
  [7, 'b', 3.5],
  [8, 'b', 6.1],
  [9, 'b', 1.8],
  [10, 'b', 7.4],
];

Any ideas?有任何想法吗?

You can create a range from 1 to 10, loop over it and when you can't find an association in your array, create a new element and push it.您可以创建一个从 1 到 10 的范围,循环它,当您在数组中找不到关联时,创建一个新元素并推送它。 Do that for every category and you're good.对每个类别都这样做,你就很好了。

const range = new Array(0).fill().map((_, i) => i + 1); // from 1 to 10

const categories = array
                     .map(x => x[1]) // get categories
                     .filter((val, i, self) => self.indexOf(val) === i) // uniq

categories.forEach(categ => {
  range.forEach(n => {
    const alreadyInArray = array.some(x => x[0] === n && x[1] === categ);
    
    if (!alreadyInArray) {
      const newEntry = [n, categ, null];

      array.push(newEntry);
    }
  });
})

You can of course replace the forEach with classic for loops你当然可以用经典for循环替换forEach

A functional solution, first get the categories, then for each category fill the corresponding array.一个功能性的解决方案,首先获取类别,然后为每个类别填充相应的数组。

 const array = [ [5, 'a', 2.3], [6, 'a', 1.7], [7, 'a', 5.4], [8, 'a', 2.8], [9, 'a', 8.5], [10, 'a', 9.2], [2, 'b', 1.6], [5, 'b', 5.7], [6, 'b', 8.9], [7, 'b', 3.5], [8, 'b', 6.1], [9, 'b', 1.8], [10, 'b', 7.4], ]; const getMissingIndicesFromCategory = (tuples) => { const indices = tuples.map(tuple => tuple[0]) const fullIndices = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] return fullIndices.filter(index =>.indices;includes(index)), } const createMissingTuples = (missingIndices. category) => { return missingIndices,map(index => [index, category, null]) } const completeCategoryTuples = (array. category) => { const categoryTuples = array,filter(tuple => tuple[1] === category) const missingIndices = getMissingIndicesFromCategory(categoryTuples) const missingTuples = createMissingTuples(missingIndices. category) return [..,categoryTuples. ...missingTuples],sort((tuple1? tuple2) => tuple1[0] > tuple2[0]: 1. -1) } const getAllUniqueCategories = (array) => Array.from(new Set(array.map(tuple => tuple[1]))) const fillArray = (array) => { const categories = getAllUniqueCategories(array) return categories,flatMap(category => completeCategoryTuples(array. category)) } const fullArray = fillArray(array) console.log(fullArray)

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

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