简体   繁体   English

使用字符串数组对 Javascript 中的对象数组进行排序

[英]Using an array of strings to sort an array of objects in Javascript

Many StackOverflow questions on sorting arrays in Javascript, however I could not find a question for this specific use case that involves a second array.关于在 Javascript 中对 arrays 进行排序的许多 StackOverflow 问题,但是我找不到涉及第二个数组的这个特定用例的问题。

I have the following 2 arrays.我有以下 2 个 arrays。 One array is the main array of data, and the other array is used to filter + sort the main array:一个数组是数据的主数组,另一个数组用于对主数组进行过滤+排序:

 let sortingArray = ['pts', 'ast', 'reb', 'stl'] let mainArray = [ { key: 'stl', val1: '3', val2: 5 }, { key: 'blk', val1: '5', val2: 1 }, { key: 'pts', val1: '23', val2: 44 }, { key: 'fgm', val1: '11', val2: 15 }, { key: 'ast', val1: '13', val2: 15 }, { key: 'reb', val1: '7', val2: 4 }, ]; // Using the 2 arrays above, I am trying to create the following output let outputArray = [ { key: 'pts', val1: '23', val2: 44 }, { key: 'ast', val1: '13', val2: 15 }, { key: 'reb', val1: '7', val2: 4 }, { key: 'stl', val1: '3', val2: 5 }, ]; // My code so far outputArray = mainArray.filter(row => sortingArray.includes(row.key)) //.sort()

I can filter, however I am not sure how to handle this sort in the best manner.我可以过滤,但是我不确定如何以最佳方式处理这种排序。 To clarify, I'd like the mainArray to be sorted based on the order of values in the sortingArray .为了澄清,我希望mainArray根据sortingArray中的值顺序进行排序。 I'm sure I could come up with an 5-10 line function that handles the sort using the sortingArray (via a loop of the array), however it would be great if there was a cleaner, 1-2 line arrow function I could use for this.我敢肯定我可以想出一个 5-10 行 function 来处理使用sortingArray进行排序(通过数组的循环),但是如果有一个更清洁的 1-2 行箭头 function 我可以用于此。 This way, I would not have to break up the chaining of functions onto the mainArray .这样,我就不必将函数链接分解到mainArray上。

Any thoughts or help on this would be great.对此的任何想法或帮助都会很棒。 I will update if I come up with an answer in the meanwhile.如果我同时想出答案,我会更新。

Edit: Creating a separate-function to sort the array is quite straightforward:编辑:创建一个单独的函数来对数组进行排序非常简单:

let outputArray = [];
sortingArray.forEach(key => {
    let obj = mainArray.filter(row => row.key === key)[0];
    outputArray.push(obj);
}); 

However an arrow function would still be preferred here.然而,箭头 function 仍然是首选。

Use maps and index使用地图和索引

sortingArray.map(y => mainArray[mainArray.map(x => x.key).indexOf(y)])

If you have for all wanted strings an object, you could take a Map and get the objects from the map.如果您有所有想要的字符串 object,您可以使用Map并从 map 获取对象。

 let sortingArray = ['pts', 'ast', 'reb', 'stl'], mainArray = [{ key: 'stl', val1: '3', val2: 5 }, { key: 'blk', val1: '5', val2: 1 }, { key: 'pts', val1: '23', val2: 44 }, { key: 'fgm', val1: '11', val2: 15 }, { key: 'ast', val1: '13', val2: 15 }, { key: 'reb', val1: '7', val2: 4 }], result = sortingArray.map( Map.prototype.get, new Map(mainArray.map(o => [o.key, o])) ); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

Could you solve the problem by inverting your operation and performing your actions on your sorting array?你能通过反转你的操作并在你的排序数组上执行你的操作来解决这个问题吗?

let sortingArray = ['pts', 'ast', 'reb', 'stl']
let mainArray = [
  { key: 'stl', val1: '3', val2: 5 },
  { key: 'blk', val1: '5', val2: 1 },
  { key: 'pts', val1: '23', val2: 44 },
  { key: 'fgm', val1: '11', val2: 15 },
  { key: 'ast', val1: '13', val2: 15 },
  { key: 'reb', val1: '7', val2: 4 },
];

// Using the 2 arrays above, I am trying to create the following output
let outputArray = [
  { key: 'pts', val1: '23', val2: 44 },
  { key: 'ast', val1: '13', val2: 15 },
  { key: 'reb', val1: '7', val2: 4 },
  { key: 'stl', val1: '3', val2: 5 },
];

outputArray = sortingArray
  .map(key => mainArray.filter((row) => key === row.key))

Create an object from the mainArray , and take the values from this object .从 mainArray 创建一个object ,并从此mainArrayobject值。 By doing this, you can avoid running the nested loops.通过这样做,您可以避免运行嵌套循环。

 let sortingArray = ['pts', 'ast', 'reb', 'stl'] let mainArray = [ { key: 'stl', val1: '3', val2: 5 }, { key: 'blk', val1: '5', val2: 1 }, { key: 'pts', val1: '23', val2: 44 }, { key: 'fgm', val1: '11', val2: 15 }, { key: 'ast', val1: '13', val2: 15 }, { key: 'reb', val1: '7', val2: 4 }, ]; function getResultant(sortingArray, mainArray) { const objFromArray = mainArray.reduce((acc, item) => { acc[item.key] = item; return acc; }, {}); return sortingArray.map(item => objFromArray[item]); } console.log(getResultant(sortingArray, mainArray));

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

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