断言已排序的对象数组
Assert sorted array of objects
我正在使用 javascript 测试框架(mocha、chai 等)进行单元测试。如何使用名称断言我的对象数组?
我可以使用 localeCompare 成功地对此进行排序,但我在测试中没有得到我想要的结果。它只是 returns 1
或 -1
.
这是我要排序的示例。
var Stuffs = [
{ name: "PWE", address: "1234567890" },
{ name: "NSA", address: "1234567890" },
{ name: "AVE", address: "1234567890" },
{ name: "QRE", address: "1234567890" },
]
我如何向 ["AVE", "NSA", "PWE", "QRE"]
断言?
要获得所需的数组,您可以使用:
Stuffs.map(({ name }) => name).sort();
使用柴
assert.deepInclude(Stuffs, {name: "AVE"});
assert.deepInclude(Stuffs, {name: "NSA"});
assert.deepInclude(Stuffs, {name: "QRE"});
assert.deepInclude(Stuffs, {name: "PWE"});
编辑:我可能误解了它的意思,如何断言数组具有这些值。
你也可以使用数组的reduce()
方法然后sort()
。
演示
var Stuffs = [{ name: "PWE", address: "1234567890" },
{ name: "NSA", address: "1234567890" },
{ name: "AVE", address: "1234567890" },
{ name: "QRE", address: "1234567890" }];
let sortedArr = Stuffs.reduce((r,{name})=>r.concat(name),[]).sort((a,b)=>a.localeCompare(b));
console.log(sortedArr);
首先,您需要一个排序数组来与之进行比较。由于 sort
will modify the array it is called on. Using from
生成克隆,因此需要克隆原始数组(对此很浅),然后我们可以按字母顺序对新数组进行排序以获得所需的顺序。
const sortedStuffs = Array.from(stuffs).sort(({name: a}, {name: b}) => a.localeCompare(b));
最后使用 every
,我们可以比较每个元素的名称以查看它们是否匹配。一旦失败,返回值将是 false
stuffs.every(({name}, i) => name === sortedStuffs[i].name);
完整的工作示例:
const stuffs = [
{name: "PWE", address: "1234567890"},
{name: "NSA", address: "1234567890"},
{name: "AVE", address: "1234567890"},
{name: "QRE", address: "1234567890"}
];
const sortedStuffs = Array.from(stuffs).sort(({name: a}, {name: b}) => a.localeCompare(b));
const isSorted = stuffs.every(({name}, i) => name === sortedStuffs[i].name);
console.log(isSorted);
我正在使用 javascript 测试框架(mocha、chai 等)进行单元测试。如何使用名称断言我的对象数组?
我可以使用 localeCompare 成功地对此进行排序,但我在测试中没有得到我想要的结果。它只是 returns 1
或 -1
.
这是我要排序的示例。
var Stuffs = [
{ name: "PWE", address: "1234567890" },
{ name: "NSA", address: "1234567890" },
{ name: "AVE", address: "1234567890" },
{ name: "QRE", address: "1234567890" },
]
我如何向 ["AVE", "NSA", "PWE", "QRE"]
断言?
要获得所需的数组,您可以使用:
Stuffs.map(({ name }) => name).sort();
使用柴
assert.deepInclude(Stuffs, {name: "AVE"});
assert.deepInclude(Stuffs, {name: "NSA"});
assert.deepInclude(Stuffs, {name: "QRE"});
assert.deepInclude(Stuffs, {name: "PWE"});
编辑:我可能误解了它的意思,如何断言数组具有这些值。
你也可以使用数组的reduce()
方法然后sort()
。
演示
var Stuffs = [{ name: "PWE", address: "1234567890" },
{ name: "NSA", address: "1234567890" },
{ name: "AVE", address: "1234567890" },
{ name: "QRE", address: "1234567890" }];
let sortedArr = Stuffs.reduce((r,{name})=>r.concat(name),[]).sort((a,b)=>a.localeCompare(b));
console.log(sortedArr);
首先,您需要一个排序数组来与之进行比较。由于 sort
will modify the array it is called on. Using from
生成克隆,因此需要克隆原始数组(对此很浅),然后我们可以按字母顺序对新数组进行排序以获得所需的顺序。
const sortedStuffs = Array.from(stuffs).sort(({name: a}, {name: b}) => a.localeCompare(b));
最后使用 every
,我们可以比较每个元素的名称以查看它们是否匹配。一旦失败,返回值将是 false
stuffs.every(({name}, i) => name === sortedStuffs[i].name);
完整的工作示例:
const stuffs = [
{name: "PWE", address: "1234567890"},
{name: "NSA", address: "1234567890"},
{name: "AVE", address: "1234567890"},
{name: "QRE", address: "1234567890"}
];
const sortedStuffs = Array.from(stuffs).sort(({name: a}, {name: b}) => a.localeCompare(b));
const isSorted = stuffs.every(({name}, i) => name === sortedStuffs[i].name);
console.log(isSorted);