简体   繁体   English

如何按另一个对象数组对数组进行排序?

[英]How can I sort an array by another array of objects?

How can I sort an array by string?如何按字符串对数组进行排序?

const filter = [
  { index: "First" },
  { index: "Second" },
  { index: "Third" }
]

const data = [
  { title: 'Apple', index: "Second" },
  { title: 'Samsung', index: "First" },
  { title: 'BMW', index: "Third" },
  { title: 'Apple', index: "Second" },
  { title: 'Apple', index: "Second" },
  { title: 'Samsung', index: "First" }
]

Expected results:预期成绩:

const data = [
  { title: 'Samsung', index: "First" },
  { title: 'Samsung', index: "First" },
  { title: 'Apple', index: "Second" },
  { title: 'Apple', index: "Second" },
  { title: 'BMW', index: "Third" }
]

How to iterate over two arrays correctly?如何正确迭代两个数组? Do I have to iterate over two arrays to do this?我是否必须遍历两个数组才能做到这一点? Or is there another way to get the desired result?还是有另一种方法可以获得所需的结果?

Given three inputs - "First", "Second", "Third", if there will be more of these indices?给定三个输入——“第一”、“第二”、“第三”,是否会有更多这些索引?

  • Using Array#reduce , iterate over filter and save each object's index attribute and its index in the array in a Map使用Array#reduce ,迭代filter并将每个对象的index属性及其在数组中的index保存在Map
  • Using Array#sort , sort data by the values of the index attributes from the above map使用Array#sort ,根据上图中的index属性值对data进行排序

 const filter = [ { index: "First" }, { index: "Second" }, { index: "Third" } ], data = [ { title: 'Apple', index: "Second" }, { title: 'Samsung', index: "First" }, { title: 'BMW', index: "Third" }, { title: 'Apple', index: "Second" }, { title: 'Apple', index: "Second" }, { title: 'Samsung', index: "First" } ]; const indexSortMap = filter.reduce((map, { index }, i) => map.set(index, i), new Map); data.sort(({ index: a }, { index: b }) => indexSortMap.get(a) - indexSortMap.get(b)); console.log(data);

Use Array#sort with custom compareFunction and Array#findIndex with custom callback functionArray#sort与自定义 compareFunction 和Array#findIndex与自定义回调函数一起使用

 const filter = [ { index: "First",}, {index: "Second",}, {index: "Third",}]; const data = [ { title: 'Apple', index: "Second" }, { title: 'Samsung', index: "First" }, { title: 'BMW', index: "Third" }, { title: 'Apple', index: "Second" }, { title: 'Apple', index: "Second" }, { title: 'Samsung', index: "First" } ]; data.sort((a ,b) => { return filter.findIndex(i => i.index === a.index) - filter.findIndex(i => i.index === b.index); }); console.log(data);

One possible solution (and simple) is by iterating these two arrays.一种可能的解决方案(并且很简单)是迭代这两个数组。 They will be pushed according to the order of the "filter" array:它们将根据“过滤器”数组的顺序推送:

const sortedArray = []
filter.forEach(fil=> {
  data.forEach(dat=>{
    if(dat.index===fil.index)
      sortedArray.push(dat)
  })
})
console.log(sortedArray)

One approach would be to push the objects to three different arrays, each one for an index and then concatenate the arrays.一种方法是将对象推送到三个不同的数组,每个数组用于一个索引,然后连接数组。

 var arr1 = [],arr2 = [],arr3 = [], data = [ { title: 'Apple', index: "Second" }, { title: 'Samsung', index: "First" }, { title: 'BMW', index: "Third" }, { title: 'Apple', index: "Second" }, { title: 'Apple', index: "Second" }, { title: 'Samsung', index: "First" } ]; for(d of data) d.index == 'First' ? arr1.push(d) : d.index == 'Second' ? arr2.push(d) : arr3.push(d); data = [].concat.apply([], [arr1, arr2, arr3]); console.log(data);

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

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