【问题标题】:Array position by object value按对象值排列位置
【发布时间】:2020-06-26 19:03:38
【问题描述】:

我想创建一个按特定键的值对数组进行排序的函数。

我会举个例子。

[{ text: 'hi', author: 'Boy' },
{ text: 'how are you', author: 'Boy' },
{ text: 'I\'m good', author: 'Boy' },
{ text: 'hello', author: 'Girl' },
{ text: 'Bye', author: 'Boy' }]

在上面的数组中,'Girl' author 比 'Boy' author 多,所以它应该返回以下数组

[{ text: 'hello', author: 'Girl' },
{ text: 'hi', author: 'Boy' },
{ text: 'how are you', author: 'Boy' },
{ text: 'I\'m good', author: 'Boy' },
{ text: 'Bye', author: 'Boy' }]

第二个例子:

[{ text: 'hi', author: 'Boy' },
{ text: 'hola', author: 'Mom' },
{ text: 'how are you', author: 'Boy' },
{ text: 'I\'m good', author: 'Boy' },
{ text: 'hello', author: 'Girl' },
{ text: 'eat this', author: 'Mom' },
{ text: 'Bye', author: 'Boy' }]

第二个结果:

[{ text: 'hello', author: 'Girl' },
{ text: 'hola', author: 'Mom' },
{ text: 'eat this', author: 'Mom' },
{ text: 'hi', author: 'Boy' },
{ text: 'how are you', author: 'Boy' },
{ text: 'I\'m good', author: 'Boy' },
{ text: 'Bye', author: 'Boy' }]

最后一个例子:

const data = [
  { text: 'hi', author: 'Boy' },
  { text: 'hola', author: 'Mom' },
  { text: 'hola', author: 'Mom' },
  { text: 'hola', author: 'Mom' },
  { text: 'how are you', author: 'Boy' },
  { text: "I'm good", author: 'Boy' },
  { text: 'hello', author: 'Girl' },
  { text: 'eat this', author: 'Mom' },
  { text: 'Bye', author: 'Boy' }
]

最后的结果(我不在乎男孩是第一个还是妈妈是第一个

const data = [
  { text: 'hello', author: 'Girl' },
  { text: 'hola', author: 'Mom' },
  { text: 'hola', author: 'Mom' },
  { text: 'hola', author: 'Mom' },
  { text: 'eat this', author: 'Mom' },
  { text: 'hi', author: 'Boy' },
  { text: 'how are you', author: 'Boy' },
  { text: "I'm good", author: 'Boy' },
  { text: 'Bye', author: 'Boy' }
]

【问题讨论】:

  • 这能回答你的问题吗? Sort array of objects by string property value
  • 您要按作者数排序吗?
  • 你能澄清一下吗?这个输入的输出是什么 `[{ text: 'hi', author: 'Boy' }, { text: 'hola', author: 'Mom' }, { text: 'how are you', author: '男孩' }, { 文字:'吃这个',作者:'妈妈' }
  • @shilu 那么如果作者数量相同呢?
  • @shilu 好的,请查看我对您帖子的回答。

标签: javascript arrays node.js sorting object


【解决方案1】:

这里是这个问题的解决方案。您可以使用此函数使用任何属性对其进行排序。

console.clear();
const data = [{ text: 'hi', author: 'Boy' },
{ text: 'hola', author: 'Mom' },
{ text: 'how are you', author: 'Boy' },
{ text: 'I\'m good', author: 'Boy' },
{ text: 'hello', author: 'Girl' },
{ text: 'eat this', author: 'Mom' },
{ text: 'Bye', author: 'Boy' }];

const sortBy = (value, data) => {
  let sortedData = [];
  const countValueObj = data.reduce((acc, obj) => {
    const authorValue = obj[value];
    if(!acc[authorValue]) acc[authorValue] = 1;
    else acc[authorValue]++;
    return acc;
  }, {})
  const countValueArr = Object.keys(countValueObj).sort((a, b) => countValueObj[a] - countValueObj[b]);
  countValueArr.forEach((val) => {
    const filteredData = data.filter((obj) => obj[value] === val);
    sortedData = [...sortedData, ...filteredData];
  })
  return sortedData;
}

const output = sortBy('author', data);

console.log(output)

【讨论】:

    【解决方案2】:

    您可以按author 分组并按计数对键进行排序并获得一个新的对象数组。

    var array = [{ text: 'hi', author: 'Boy' }, { text: 'hola', author: 'Mom' }, { text: 'how are you', author: 'Boy' }, { text: 'I\'m good', author: 'Boy' }, { text: 'hello', author: 'Girl' }, { text: 'eat this', author: 'Mom' }, { text: 'Bye', author: 'Boy' }],
        temp = array.reduce((r, o) => {
            if (!r[o.author]) r[o.author] = [];
            r[o.author].push(o);
            return r;
        }, {}),
        result = Object
            .keys(temp)
            .sort((a, b) => temp[a].length - temp[b].length)
            .flatMap(k => temp[k]);
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      【解决方案3】:

      如果您修改数据集并向其添加计数,则以下代码应该可以完成这项工作。

      var data = [{ text: 'hi', author: 'Boy' },
      { text: 'hola', author: 'Mom' },
      { text: 'how are you', author: 'Boy' },
      { text: 'I\'m good', author: 'Boy' },
      { text: 'hello', author: 'Girl' },
      { text: 'eat this', author: 'Mom' },
      { text: 'Bye', author: 'Boy' }];
      
      // add counts against each object
      data.forEach(obj => {
        obj['count'] = data.filter((obj1) => obj1.author === 
          obj.author).length;
      })
      
      // user Array.sort function to sort your data
      data.sort(function(a, b){
          if(a.count < b.count) return -1;
          if(a.count > b.count) return 1;
          return 0;
      });
      

      不过,如果你从后端得到这个列表会更好。

      【讨论】:

      • @shilu 你能提供你测试的数据集吗?
      【解决方案4】:

      你可以使用

      array.sort((a, b) => {
        return a.author - b.author;
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        • 2018-03-15
        • 1970-01-01
        • 1970-01-01
        • 2016-05-02
        • 1970-01-01
        • 2019-08-17
        相关资源
        最近更新 更多