获取数组的每四分之一并检查

时间:2019-05-22 23:39:44

标签: javascript arrays

我有这样的数组:

In [2]: eq1                                                                                                                       
Out[2]: 
d ⎛        d        ⎞   d        
──⎜x₃(t) + ──(x₃(t))⎟ + ──(x₃(t))
dt⎝        dt       ⎠   dt       

In [3]: eq1.doit()                                                                                                                
Out[3]: 
                2       
  d            d        
2⋅──(x₃(t)) + ───(x₃(t))
  dt            2       
              dt  

我想要实现的是获得四分之一,获得特殊的一个(带有怪物对象的那个),最后获得一个。因此输出将是

let arr = [ 
  { x: 31, y: 8 }, // get 1
  { x: 32, y: 8, monster: { is: true, id: '19216' } }, // get special
  { x: 32, y: 9 },
  { x: 32, y: 10 },
  { x: 32, y: 11 }, // get 4
  { x: 32, y: 12 },
  { x: 32, y: 13 },
  { x: 32, y: 14 },
  { x: 32, y: 15 }, // get 8
  { x: 32, y: 16 }  // get last
];

很容易获得这样的第四和最后一个:

[
 {x: 31, y: 8},
 {x: 32, y: 8, monster: { is: true, id: '19216' } },
 {x: 32, y: 11},
 {x: 32, y: 15},
 {x: 32, y: 16}
]

但是我不知道如何另外检查是否每四分之一有这个特殊的beetwen并将其添加到thinnedArr数组中。我需要保持秩序。 Demo以上代码。

2 个答案:

答案 0 :(得分:4)

在这里,使用filter

let arr = [ 
  { x: 31, y: 8 }, // get 1
  { x: 32, y: 8, monster: { is: true, id: '19216' } }, // get special
  { x: 32, y: 9 },
  { x: 32, y: 10 },
  { x: 32, y: 11 }, // get 4
  { x: 32, y: 12 },
  { x: 32, y: 13 },
  { x: 32, y: 14 },
  { x: 32, y: 15 }, // get 8
  { x: 32, y: 16 }  // get last
];

let newArr = arr.filter((e,i,ar) => (i%4 === 0 || e.monster || i===ar.length-1));

console.log(newArr);

答案 1 :(得分:1)

使用.flatMap()整理所有必需的对象。基于此answer的自定义函数计算每个对象具有多少个键,以便如果一个对象具有两个以上的键,则认为它是特殊键。demo中的版本简化为一行。

/** objSize(object)
Utility that returns a given Object's number of non-enumerated property keys it
has (String and Symbol). 
*/
const objSize = obj => {
  let strings = Object.getOwnPropertyNames(obj).length;
  let symbols = Object.getOwnPropertySymbols(obj).length;
  return strings + symbols;
}

有关.flatMap()的更多详细信息在演示中进行了评论。

let array = [ 
  { x: 31, y: 8 }, // get 1
  { x: 32, y: 8, z: { is: true, id: '19216' } }, 
  { x: 32, y: 9 },
  { x: 32, y: 10 },
  { x: 32, y: 11 }, // get 4
  { x: 32, y: 12 },
  { x: 32, y: 13 },
  { x: 32, y: 14 },
  { x: 32, y: 15 }, // get 8
  { x: 32, y: 16 }  // get last
];

const objSize = obj => Object.getOwnPropertyNames(obj).length + Object.getOwnPropertySymbols(obj).length;

/*
.flatMap() is an array method that is basically a
combonation of `.map()` and `.flat()`. Here it is running 
a function of 4 ternary controls:
1. if current index is 0: 
   (idx === 0) 
   return [xy]
2. if current index is a factor of 4: 
   (idx % 4 === 0)
   return [xy]
3. if current xy has more than 2 keys:  
   (objSize(xy) > 2)
   return [xy]
4. if current index is the last:
   (idx === array.length - 1)
   return [xy]
5. otherwise return []
each return is an array which is flattened when the final 
array is returned. Therefore an empty array is a clean
removal which means no nulls, spaces, or empty values.
*/
let result = array.flatMap((xy, idx) => xy === 0 ? [xy] : idx % 4 === 0 ? [xy] : objSize(xy) > 2 ? [xy] : idx === array.length - 1 ? [xy] : []);

console.log(JSON.stringify(result));