根据另一个数组对对象数组进行排序

时间:2019-07-01 12:54:19

标签: javascript arrays object lodash

我有一些品牌,如下所示:

const makes = [
{id: "4", name: "Audi"},
{id: "5", name: "Bmw"},
{id: "6", name: "Porsche"},
{id: "31", name: "Seat"},
{id: "32", name: "Skoda"},
{id: "36", name: "Toyota"},
{id: "38", name: "Volkswagen"}
]

我想基于另一个数组对该数组进行排序:

const preferred_makes = ['Volkswagen', 'Audi'];

我现在要做的是:

const preferred_makes = ['Volkswagen', 'Audi'];

const makes = [
{id: "4", name: "Audi"},
{id: "5", name: "Bmw"},
{id: "6", name: "Porsche"},
{id: "31", name: "Seat"},
{id: "32", name: "Skoda"},
{id: "36", name: "Toyota"},
{id: "38", name: "Volkswagen"}
]

const mainMakes = []
const otherMakes = []

makes.map(make => _.includes(preferred_makes, make.name) ? mainMakes.push(make) : otherMakes.push(make))

console.log(mainMakes)
console.log(otherMakes)

但是还有什么更好的方法吗?我可以对makes进行排序以将那些preferred_makes显示为数组的第一个元素吗?

Here is the fiddle.

5 个答案:

答案 0 :(得分:2)

您可以采用索引增加一个的对象,并为未找到的名称采用默认值Infinity。然后按值的增量排序。

var preferred_makes = ['Volkswagen', 'Audi'],
    preferred = preferred_makes.reduce((o, k, i) => (o[k] = i + 1, o), {});
    array = [{ id: "4", name: "Audi" }, { id: "5", name: "Bmw" }, { id: "6", name: "Porsche" }, { id: "31", name: "Seat" }, { id: "32", name: "Skoda" }, { id: "36", name: "Toyota" }, { id: "38", name: "Volkswagen" }];

array.sort((a, b) => (preferred[a.name] || Infinity) - (preferred[b.name] || Infinity));

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:2)

具有自定义比较功能的常规array.sort()应该能够做到这一点。

const preferred_makes = ['Volkswagen', 'Audi'];

const makes = [
  {id: "4", name: "Audi"},
  {id: "5", name: "Bmw"},
  {id: "6", name: "Porsche"},
  {id: "31", name: "Seat"},
  {id: "32", name: "Skoda"},
  {id: "36", name: "Toyota"},
  {id: "38", name: "Volkswagen"}
]

const sorted = makes.slice().sort((a, b) => {
  // Convert true and false to 1 and 0
  const aPreferred = new Number(preferred_makes.includes(a.name))
  const bPreferred = new Number(preferred_makes.includes(b.name))
  
  // Return 1, 0, or -1
  return bPreferred - aPreferred
})

console.log(sorted)

答案 2 :(得分:1)

您可以使用reduce来创建两个数组,而无需进行排序:

const preferred_makes = ['Volkswagen','Audi'];
const makes = [{id:"4",name:"Audi"},{id:"5",name:"Bmw"},{id:"6",name:"Porsche"},{id:"31",name:"Seat"},{id:"32",name:"Skoda"},{id:"36",name:"Toyota"},{id:"38",name:"Volkswagen"}];

const [mainMakes, otherMakes] = makes.reduce(([a, b], { id, name }) => ((preferred_makes.includes(name) ? a : b).push({ id, name }), [a, b]), [[], []]);

console.log(mainMakes);
console.log(otherMakes);
.as-console-wrapper { max-height: 100% !important; top: auto; }

要使其更快,您可以使用Set.prototype.has代替includes

const preferred_makes = new Set(['Volkswagen','Audi']);
const makes = [{id:"4",name:"Audi"},{id:"5",name:"Bmw"},{id:"6",name:"Porsche"},{id:"31",name:"Seat"},{id:"32",name:"Skoda"},{id:"36",name:"Toyota"},{id:"38",name:"Volkswagen"}];

const [mainMakes, otherMakes] = makes.reduce(([a, b], { id, name }) => ((preferred_makes.has(name) ? a : b).push({ id, name }), [a, b]), [[], []]);

console.log(mainMakes);
console.log(otherMakes);
.as-console-wrapper { max-height: 100% !important; top: auto; }

答案 3 :(得分:0)

如果索引在那里,您也可以按Array.indexOf排序,否则使用String.localeCompare。真的不需要lodash:

const makes = [ {id: "4", name: "Audi"}, {id: "6", name: "Porsche"}, {id: "31", name: "Seat"}, {id: "32", name: "Skoda"}, {id: "5", name: "Bmw"}, {id: "36", name: "Toyota"}, {id: "38", name: "Volkswagen"} ] 
const list = ['Volkswagen', 'Audi'];

let result = makes.sort((a,b) => {
  let i = list.indexOf(a.name)
  return i < 0 ? a.name.localeCompare(b.name) : list.indexOf(b.name) - i
})

console.log(result)

答案 4 :(得分:0)

使用lodash,您可以使用index通过汽车品牌(indexByMake)来生成原始_.invert()的字典,以获取{ [car make]: original array index }的对象,并映射值回到数字。

使用_.orderBy()对数组进行排序,并根据indexByMake使用name中的值:

const preferred_makes = ['Volkswagen', 'Audi'];
const array = [{ id: "4", name: "Audi" }, { id: "5", name: "Bmw" }, { id: "6", name: "Porsche" }, { id: "31", name: "Seat" }, { id: "32", name: "Skoda" }, { id: "36", name: "Toyota" }, { id: "38", name: "Volkswagen" }];

const indexByMake = _.mapValues(_.invert(preferred_makes), Number);

const result = _.sortBy(array, ({ name }) => indexByMake[name]);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>