简体   繁体   English

根据作为正则表达式的属性对对象数组进行排序

[英]Sorting an Array of objects based on a property as a Regular Expression

If I have an array of words...如果我有一组单词...

const a = ["abc", "abcd", "xyz"]

and I want to sort that array and filter it based off a pattern (ex. "ab") then I can use filter and regex like so... [giving me the array of items which has "ab" at the start of every item in the array"我想对该数组进行排序并根据模式(例如“ab”)对其进行过滤,然后我可以像这样使用过滤器和正则表达式...... [给我在每个开头都有“ab”的项目数组数组中的项目”

let value = "abc";
let regex = new RegExp(`^${value}`, `i`);
const sorted = a.sort().filter(v => regex.test(v));

However, what about this same principle, but instead of an array of items, those items are objects within an array, like so...然而,同样的原理呢,但不是一个项目数组,这些项目是一个数组中的对象,就像这样......

const b = [
{text: "abc", color: "blue"},
{text: "abcd", color: "red"},
{text: "xyz", color: "yellow"}
]

You should first filter() the array based on the regex (there is no need to sort elements that will be filtered later) and then sort it using String::localeCompare()您应该首先根据regex filter()数组(无需对稍后将被过滤的元素进行排序),然后使用String::localeCompare()对其进行排序

 const arr = [ {text: "abcd", color: "red"}, {text: "abc", color: "blue"}, {text: "xyz", color: "yellow"} ]; let value = "abc"; let regex = new RegExp(`^${value}`, `i`); const sortedArr = arr .filter(x => regex.test(x.text)) .sort((a, b) => a.text.localeCompare(b.text)); console.log(sortedArr);
 .as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;}

you'll need to tell .sort() which property you want to sort with and pass v.text to the regex :您需要告诉.sort()您要使用哪个属性进行排序并将v.text传递给正则表达式:

 const b = [{ text: "abc", color: "blue" }, { text: "abcd", color: "red" }, { text: "xyz", color: "yellow" } ] let value = "abc"; let regex = new RegExp(`^${value}`, `i`); const sorted = b.sort((a, b) => { if (a.text > b.text) return 1; if (a.text < b.text) return -1; return 0 }).filter(v => regex.test(v.text)); console.log(sorted);

You can create a function which accepts input array, string, and a function which accepts the current value of .findIndex() to sort the resulting array and the string value which is passed to RegExp constructor, where the function parameters can be adjusted to use destructuring assignment to match specific property name within input array of objects, with .splice() used to remove matched element from array, or remainder of input array.您可以创建一个接受输入数组、字符串的函数和一个接受.findIndex()的当前值的.findIndex()来对结果数组和传递给RegExp构造函数的字符串值进行排序,其中可以调整函数参数以使用解构赋值以匹配输入对象数组中的特定属性名称,使用.splice()从数组或输入数组的其余部分中删除匹配的元素。

 const regexSort = (arr, value, fn) => { const res = []; for (let i; (i = arr.findIndex(v => fn(v, value))) > -1; res.push(arr.splice(i, 1)[0])); return [...res, ...arr]; } const a = ["abc", "abcd", "xyz", "abcdef", "qrst", "abcg"]; const b = [ {text: "abc", color: "blue"}, {text: "abcd", color: "red"}, {text: "xyz", color: "yellow"}, {text: "abcdef", color: "gold"}, {text: "qrst", color: "green"}, {text: "abcg", color: "brown"} ]; let value = "abc"; // array of strings console.log( regexSort(a, value, (key, value) => new RegExp(`^${value}`, `i`).test(key)) ); // array of objects, destructure `text` property assign to `key` console.log( regexSort(b, value, ({text: key}, value) => new RegExp(`^${value}`, `i`).test(key)) );

(Filtering first may avoid sorting a lot of value that would be discard by filter) (先过滤可以避免对大量会被过滤器丢弃的值进行排序)

 const b = [ {text: "abcf", color: "white"}, {text: "abc", color: "blue"}, {text: "xyz", color: "yellow"}, {text: "abcd", color: "red"}, {text: "abcz", color: "green"}, ]; let value = "abc"; let regex = new RegExp(`^${value}`, `i`); const sorted = b .filter(item => regex.test(item.text)) .sort((a, b) => a.text.localeCompare(b.text)); console.log(sorted);

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

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