简体   繁体   English

打字稿减去两个布尔

[英]Typescript subtract two boolean

I have an array : 我有一个数组:

main = [{
           data: x,
           numberField:  1;
 }, {      data: y,
           numberField:  2;
 }, {      data: x,
           numberField:  3;
 },
  {        data:z
           numberField:  4;
 },
    {      data: q
           numberField:  5;
 }]
fixedElements = [ 3, 5]

I need to have : 我需要:

 fixedElements .includes(a.numberField) - fixedElements .includes(b.numberField) 

Because I need to check whether two coming values are in the array a and not sort them, but sort the rest. 因为我需要检查数组a中是否有两个即将到来的值,而不是对它们进行排序,而是对其余值​​进行排序。 when I try to do this typescript throws an error 当我尝试执行此打字稿时会引发错误

  left/right hand-side of the argument needs to be of type any or number.

My initial sort function as : 我最初的排序函数为:

  sort(a,b) {
  if(a.numberField > b.numberField)
       return -1

  if(a.numberField < b.numberField)
     return 1;

    }

So I wanted to check whether the coming a or b are in the fixedElements array, and leave them last. 所以我想检查即将到来的a或b是否在fixedElements数组中,并将它们留在最后。 Is there any other way to do this? 还有其他方法吗?

You can cast the booleans to numbers by using the + sign ( typescript playground demo ): 您可以使用+符号( 打字机游乐场演示 )将布尔值转换为数字:

+fixedElements.includes(a.numberField) - +fixedElements.includes(b.numberField)`

And this is the sort logic ( typescript playgroud - check console): 这是排序逻辑( 打字稿playgroud-检查控制台):

 const main = [{"data":"x","numberField":1},{"data":"y","numberField":2},{"data":"x","numberField":3},{"data":"z","numberField":4},{"data":"q","numberField":5}] // I'm using a Set instead of an array, because Set.has is faster than Array.includes const fixedElements = new Set([3, 5]) main.sort((a, b) => { const aF = fixedElements.has(a.numberField) const bF = fixedElements.has(b.numberField) if(!aF && !bF) return b.numberField - a.numberField return +aF - +bF }) console.log(main) 

You can use a custom sort function and check if the numberField value exists in the fixedElements array and leave them unsorted, otherwise sort them based on the numberField value. 您可以使用自定义排序功能,并检查fixedElements数组中是否存在numberField值, fixedElements它们保持未排序状态,否则根据numberField值对其进行排序。

 let main = [{ data: 'x', numberField: 1}, { data: 'y', numberField: 2}, { data: 'x', numberField: 3}, { data:'z', numberField: 4}, { data: 'q', numberField: 5}], fixedElements = [3, 5]; main.sort((a,b) => { if([a,b].some(({numberField}) => fixedElements.includes(numberField))) return +fixedElements.includes(a.numberField) - +fixedElements.includes(b.numberField); else return +b.numberField - +a.numberField }); console.log(main); 

You can check using includes() method inside of sort method. 您可以在sort方法中使用includes()方法进行检查。

 var array = [{ data: 'x', numberField: 1 }, { data: 'y', numberField: 2 }, { data: 'x', numberField: 3 }, { data: 'z', numberField: 4 }, { data: 'q', numberField: 5 }], fixedElements = [3, 5]; array.sort((a, b) => { a = +a.numberField; b = +b.numberField; return (fixedElements.includes(a) - fixedElements.includes(b) || b - a); }); console.log(array) 

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

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