按其值排序Array元素

问题描述 投票:1回答:4

我有一个数组元素需要排序并将选定的元素放入数组的顶部。

[{
    parent_email: '[email protected]',
    id: 143,
    unreadCount: 0 
 },
 {
    parent_email: '[email protected]',
    id: 210,
    unreadCount: 0 
 },
 {
    parent_email: '[email protected]',
    id: 225,
    unreadCount: 0 
 },
 {
    parent_email: '[email protected]',
    id: 221,
    unreadCount: 0 
 },
 {
    parent_email: '[email protected]',
    id: 224,
    unreadCount: 0 
 }]

我有另一个数组,上面的数组元素需要排序。第一个元素在第二个位置在第二个位置第三个位于第三个位置,依此类推。

[{ 
    parent_id: '[email protected]'
 },
 { 
    parent_id: '[email protected]'
 },
 { 
    parent_id: '[email protected]'
 }]

我的结果数组应该是这样的

[{
    parent_email: '[email protected]',
    id: 221,
    unreadCount: 0 
 },
 {
    parent_email: '[email protected]',
    id: 225,
    unreadCount: 0 
 },
 {
    parent_email: '[email protected]',
    id: 210,
    unreadCount: 0 
 },
 {
    parent_email: '[email protected]',
    id: 143,
    unreadCount: 0 
 },
 {
    parent_email: '[email protected]',
    id: 224,
    unreadCount: 0 
 }]

我试过但它只排序单个元素而不是一个。

for (var i = array2.length - 1; i >= 0; i--) {
array1.sort(function(x,y){
    return x.parent_email == rows[i].parent_id ? -1 : y.parent_email ==  rows[i].parent_id ? 1 : 0; 
    });
}
javascript jquery node.js
4个回答
0
投票

对我来说,它似乎不像排序(因为你按照所需的顺序抓取匹配的项目,而不关心其余的顺序),所以你可能会去(比如,它可能会产生意想不到的输出,如果重复e-电子邮件):

const src = [
  {parent_email: '[email protected]', id: 221, unreadCount: 0},
  {parent_email: '[email protected]', id: 225, unreadCount: 0},
  {parent_email: '[email protected]', id: 210, unreadCount: 0},
  {parent_email: '[email protected]', id: 143, unreadCount: 0},
  {parent_email: '[email protected]', id: 224, unreadCount: 0}
];
const order = [
  {parent_id: '[email protected]'},
  {parent_id: '[email protected]'},
  {parent_id: '[email protected]'}
];
const res = order
  .map(item => src.find(entry => entry.parent_email == item.parent_id))
  .concat(src.filter(entry => !order.map(item => item.parent_id).includes(entry.parent_email)));

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

2
投票

您应该按第二个数组中的索引对第一个数组进行排序:

 const emailRank = new Map();
 for(const [index, { parent_id }] of array2.entries())
   emailRank.set(parent_id, index);

 array1.sort((a, b) => emailRank.get(b.parent_email) - emailRank.get(a.parentEmail));

0
投票

尝试在lodash中排序

_.sortBy( parent_email, [function(o) { return o.parent_email; }]);

0
投票

const data = [{
    parent_email: '[email protected]',
    id: 143,
    unreadCount: 0
  },
  {
    parent_email: '[email protected]',
    id: 210,
    unreadCount: 0
  },
  {
    parent_email: '[email protected]',
    id: 225,
    unreadCount: 0
  },
  {
    parent_email: '[email protected]',
    id: 221,
    unreadCount: 0
  },
  {
    parent_email: '[email protected]',
    id: 224,
    unreadCount: 0
  }
]
const orderBy = [{
    parent_id: '[email protected]'
  },
  {
    parent_id: '[email protected]'
  },
  {
    parent_id: '[email protected]'
  }
]
const length = data.length
const dataOrders = orderBy.reduce((result, item, index) => (result[item.parent_id] = index + 1) && result, {})
const dataOrdered = data.sort((a, b) => (dataOrders[a.parent_email] || length) - (dataOrders[b.parent_email] || length))
console.log(dataOrdered)
© www.soinside.com 2019 - 2024. All rights reserved.