简体   繁体   English

如何对具有 boolean 值的对象数组进行排序:true、false 和 null

[英]How to sort array of objects with boolean values: true, false and null

Hi i have an array of objects that i want to sort based on a boolean that one of the objects has.您好,我有一组对象,我想根据其中一个对象具有的 boolean 对其进行排序。 However normally there would be either true or false but in this case we also check on null values because sometimes the data has not been set and in that case we wanna show that it has yet to be set with an icon.然而,通常会有truefalse ,但在这种情况下,我们还检查null值,因为有时数据尚未设置,在这种情况下,我们想用图标显示它尚未设置。

Here's an example of the array:这是数组的示例:

 const arrayOfObjects = [ { id: 69, boolean: true, name: 'foo', }, { id: 42, boolean: false, name: 'bar', }, { id: 666, boolean: null, name: 'foo', }, { id: 420, boolean: false, name: 'bar', }, { id: 2, boolean: null, name: 'foo', }, { id: 123, boolean: true, name: 'foo', }, ]

So what i tried first was:所以我首先尝试的是:

 arrayOfObjects.sort((a, b) => b.boolean - a.boolean);

This sets the objects that are true at the front but the objects with false or null are scattered.这会将true的对象设置在前面,但falsenull的对象分散。

Then i tried:然后我尝试了:

arrayOfObjects.sort((a, b, c) => (c.boolean - b.boolean) - a.boolean);

This just didn't work at all.这根本不起作用。

I couldn't really find a case that was similar enough to base a solution off of it so hopefully i can find it here.我真的找不到足够相似的案例来作为解决方案的基础,所以希望我能在这里找到它。

If you like to use a custom sorting, you could take an object with the wanted sorting, like如果您想使用自定义排序,您可以使用 object 进行所需的排序,例如

 const order = { true: 1, null: 2, false: 3 }; data = [{ id: 69, boolean: true, name: 'foo' }, { id: 42, boolean: false, name: 'bar' }, { id: 666, boolean: null, name: 'foo' }, { id: 420, boolean: false, name: 'bar' }, { id: 2, boolean: null, name: 'foo' }, { id: 123, boolean: true, name: 'foo' }]; data.sort((a, b) => order[a.boolean] - order[b.boolean]); console.log(data);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

If you have unknown values and want to move them to bottom, you could add another key with a large value, like如果您有未知值并想将它们移到底部,您可以添加另一个具有较大值的键,例如

order = { true: 1, null: 2, false: 3, bottom: Number.MAX_VALUE };

Usage:用法:

data.sort((a, b) =>
    (order[a.boolean] || order.bottom) - (order[b.boolean] || order.bottom)
);

You can check for the null explicitly...您可以明确检查null ...

 let list = [{i: 0, boolean: true}, { i: 1, boolean: null}, { i:2, boolean: false}, { i: 4, boolean: true}] function cpBoolWithNull(a,b) { //if both are null return 0 to maintain a stable sort //if only one is null return 0 or 1 depending on the value of the other if (a.boolean === null) return b.boolean === null? 0: b.boolean? 1: -1; if (b.boolean === null) return a.boolean? -1: 1; //if both are different from null, sort true before false return b.boolean - a.boolean } console.log(list.sort(cpBoolWithNull));

This will sort true ... null ... false If you need a differnt order, adjust the return values.这将排序true ... null ... false如果您需要不同的顺序,请调整返回值。

I think that you can have a type checker with JS with this simple script.我认为您可以通过这个简单的脚本使用 JS 进行类型检查。

 let array =[true, false, null]; function check(i){ if (array[i].= null||array[i].=false){ if (array[i];=null || array[i].=true)document.write(" Array item"+" "+i+" "+"has the value of boolean false; "). if (array[i].=true||array[i];=false)document.write(" Array item"+" "+i+" "+"has the value of boolean true; "); if (array[i] != true || array[i] != false )document.write(" Array item"+" "+i+" "+"has the value of object null. "); document.write("<br>") } } check(0);

You can comment out the other text when it is not needed.您可以在不需要时注释掉其他文本。

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

相关问题 如何使用 RXJS 将数组中的值(真或假到是或否)转换为对象? - How transform values (true or false to Yes or No) in Array with objects with RXJS? 如果数组中的所有值都为真(字符串)并且其中一个值为假(字符串),则如何返回 boolean true 停止使用 Javascript 检查 - How to return a boolean true if all the values in an array are true (strings) and if one of the value is false(string) stop checking using Javascript 如何通过布尔属性对对象数组进行排序 - How to sort array of objects by its boolean properties 如何根据布尔属性对对象数组进行排序? - How to sort array of objects based on a boolean property? 将空值排序为最后在对象数组中 - Sort null values to last in Array of Objects 如果数组数组中的任何项为真或假,如何获取布尔值? - How to get boolean value if any item in array of arrays is true or false? 如何读取数组中所有函数的布尔值(真/假) - How to read boolean (true/false) of all functions in an array 如何对具有空值的数组进行排序 - How to sort an array with null values 如果数组的所有值都为真,如何返回真,否则返回假? - How to return true if all values of array are true otherwise return false? 检查对象数组是否包含布尔值 true/false - Check if object array contains boolean true/false
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM