【问题标题】:Sort an array of objects with another array使用另一个数组对对象数组进行排序
【发布时间】:2019-12-04 14:33:09
【问题描述】:

我有一个这样的对象数组,想用另一个对象数组重新排序。我曾尝试使用 indexOf 但可能会混淆我的语法,因为数组无法重新排序。我已经阅读了类似的问题,但无法将这些解决方案应用于我的问题。这是代码:

    const task = [
       {'title':1.1 , 'description': 'task description here' },
       {'title':1.2 , 'description': 'task description here' },
       {'title':1.3 , 'description': 'task description here' },
       {'title':1.4 , 'description': 'task description here' }
    ];

    var taskSort = [
            {'title':1.2 },
            {'title':1.4 },
            {'title':1.3 },
            {'title':1.1 }
        ];

    task.sort(function(a, b) {
      return taskSort.indexOf(a.title) - taskSort.indexOf(b.title); \\have tried many variations of this line
    });
    console.clear();
    console.log(task);

非常感谢!

【问题讨论】:

标签: javascript arrays sorting object


【解决方案1】:

基本上,您不是对值进行排序,而是根据另一个数组中指定的顺序重新排列它们

因此,您不能使用Array.prototype.sort 逻辑,但可以执行以下操作

var taskSort = [
            {'title':1.2 },
            {'title':1.4 },
            {'title':1.3 },
            {'title':1.1 }
        ];
   var task = [
       {'title':1.1 , 'description': 'task description here' },
       {'title':1.2 , 'description': 'task description here' },
       {'title':1.3 , 'description': 'task description here' },
       {'title':1.4 , 'description': 'task description here' }
    ];
   var sortedTask = taskSort.map(tS => task.find(t => t.title === tS.title))

console.log(sortedTask);

本质上,您是通过 taskSort 数组进行映射并创建一个新数组,其中的值满足 taskSort 数组中的值标记的条件

【讨论】:

    【解决方案2】:

    您可以使用所需的title 顺序构建一个对象,并获取值的增量进行排序。

    通过采用这种方法,如果不知道 title 值,您可以将值添加为默认值。然后你可以使用Number.MAX_VALUE 将这些项目移到底部或使用-Number.MAX_VALUE 移到顶部,例如

    (order[a] || -Number.MAX_VALUE) - (order[b] || -Number.MAX_VALUE) //   top 
    (order[a] || Number.MAX_VALUE) - (order[b] || Number.MAX_VALUE)  // bottom
    

     var task = [{ title: 1.1, description: 'task description here' }, { title: 1.2, description: 'task description here' }, { title: 1.3, description: 'task description here' }, { title: 1.4, description: 'task description here' }];
        taskSort = [ { title: 1.2 }, { title: 1.4 }, { title: 1.3 }, { title: 1.1 }],
        order = Object.fromEntries(taskSort.map(({ title }, i) => [title, i + 1]));
    
    task.sort(({ title: a }, { title: b }) => order[a] - order[b]);
    
    console.log(task);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      【解决方案3】:

      您没有得到预期的结果,因为您正在检查对象数组 (taskSort) 中的 int (a.title) 的索引。所以由于没有这样的元素,taskSort.indexOf(a.title) 总是返回-1。最简单的方法是像这样修改任务排序:

      var taskSort = [1.2, 1.4, 1.3, 1.1];
      

      一切都应该按预期工作。

      【讨论】:

        【解决方案4】:

        您的实现实际上非常接近,我认为您只需要使用 findIndex 而不是 indexOf 即可:

        return taskSort.findIndex(element => element.title === a.title) - taskSort.findIndex(element => element.title === b.title);
        

        indexOf 尝试将您的输入与数组的确切元素匹配,而不是与元素的属性匹配。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-20
          • 2016-06-02
          • 1970-01-01
          相关资源
          最近更新 更多