简体   繁体   English

根据其他对象的array属性对对象数组进行排序的最快方法

[英]Fastest way to sort object's array based on other's object's array property

I've got two objects arrays and both of them have columnId property. 我有两个对象数组,它们都具有columnId属性。 I want to make the order of the first object's array same as the order of the second. 我想使第一个对象的数组的顺序与第二个对象的顺序相同。

I've tried this: 我已经试过了:

filtered = visibleColumns.filter(function(v) {
            return filtered.includes(v.colId);
        });

where filtered is my result array and visibleColumns is array which order I need, but it doesn't work. 我的结果数组filtered在哪里,而visibleColumns是我需要的顺序排列的数组,但这不起作用。

example of arrays: 数组的例子:

 filtered = [{ colId:1, title: 'col1', size: 10 }, { colId:2, title: 'col2', size: 10 }]; visibleColumns = [{ colId:2, visible: true }, { colId:1, visible: true }]; 

You could create a Map object which maps each colId from visibleColumns to it's index in the array. 您可以创建一个Map对象,将每个colIdvisibleColumns映射到数组中的索引。 Get the index for each colId while sorting filtered 获取每个目录colId排序时filtered

 const filtered = [{ colId: 1, title: "col1", size: 10 }, { colId: 2, title: "col2", size: 10 }], visibleColumns = [{ colId: 2, visible: true }, { colId: 1, visible: true }]; const order = new Map(visibleColumns.map((o, i) => [o.colId, i])) filtered.sort((a, b) => order.get(a.colId) - order.get(b.colId)) console.log(filtered) 

You could create an object with the wanted order and take a default value for unknown id for sorting them to bottom. 您可以创建具有所需顺序的对象,并为未知ID取默认值以将其排序到最底端。

 var filtered = [{ colId: 1, title: 'col1', size: 10 }, { colId: 2, title: 'col2', size: 10 }], visibleColumns = [{ colId: 2, visible: true }, { colId: 1, visible: true }], order = visibleColumns.reduce((o, { colId }, i) => (o[colId] = i + 1, o), {}); filtered.sort((a, b) => (order[a.colId] || Infinity) - (order[b.colId] || Infinity)); console.log(filtered); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

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

相关问题 根据对象的属性以自定义方式排序 JavaScript 对象数组 - Sort JavaScript array of Objects based on the object's properties in custom way 从具有特定属性的数组中删除对象的最快方法是什么? - What's the fastest way to remove an object from an array that has a specific property? 根据对象的值将javascript对象键排序到数组中 - Sort javascript object key into array based on object's value 如何根据其他 object 修改对象数组中的值 - How to modify a value in array of object's based on other object 将属性限制为其他属性的 object 数组中定义的值? - Restrict property to values defined in other property's object array? 过滤基于 object 的对象数组,其中数组作为其属性值在反应 - filter an array of objects based on an object with an array as it's property value in react 更改javascript对象数组中的对象属性的最佳方法是什么? - what's the best way to change an object property in a javascript object array? 如何根据对象的值对对象数组进行分组排序? - How grouped sort array of objects based on object's value? 根据对象的属性值(相对于其他元素中发生的次数)过滤数组 - Filter array based on an object's property value relative to how many times it occurs in other elements 将“程序化”数组推送到对象的array属性 - Push “programmatic” array to an Object's array property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM