[英]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.然而,通常会有
true
或false
,但在这种情况下,我们还检查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
的对象设置在前面,但false
或null
的对象分散。
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.