【问题标题】:Javascript simple object sortingJavascript简单对象排序
【发布时间】:2019-11-14 16:06:49
【问题描述】:

我对 JavaScript 很陌生。最近在学习数组方法。

我要做什么

如何通过使用一些数组方法(过滤器、映射等)或其他任何可行的方法将问题对象转换为结果对象?

我已经尝试过的工作示例

let question = {
  rows: [
    { no: 1, content: "a" },
    { no: 2, content: "d" },
    { no: 1, content: "b" },
    { no: 3, content: "e" },
    { no: 2, content: "c" },
    { no: 4, content: "f" },
    { no: 4, content: "g" },
  ]
};


let results = {
    rows: [
        [{ no: 1, content: "a" }, { no: 1, content: "b" }],
        [{ no: 2, content: "c" }, { no: 2, content: "d" }],
        [{ no: 3, content: "e" }],
        [{ no: 4, content: "f" }, { no: 4, content: "g" }]
    ] 
}

预期结果

实际结果

【问题讨论】:

  • 你会如何解决这个问题?你有什么想法吗?有什么尝试吗?
  • 尽管您是 JS 新手,但在 StackOverflow 中,我们希望寻求解决方案的人付出最小的努力,因为我们不是来编写完整的解决方案/免费代码,我们来这里是为了帮助。因此,请在您尝试解决的地方发布您当前的代码,我们很乐意为您提供帮助
  • 对不起,伙计们。这也是我第一次在 StackOverflow 上提问。我得到的唯一想法是我只知道也许我需要使用过滤器和映射数组方法。如果我有一些想法,我会更新一些代码。
  • 想想你想做什么,你想把问题行数组中的每个对象都添加到结果行数组中吗?
  • 你可以简单地交换它们

标签: javascript arrays sorting object filter


【解决方案1】:

可以做这样的事情

let question = {
  rows: [
    { no: 1, content: "a" },
    { no: 2, content: "d" },
    { no: 1, content: "b" },
    { no: 3, content: "e" },
    { no: 2, content: "c" },
    { no: 4, content: "f" },
    { no: 4, content: "g" }
  ]
};

// Create a result Object
let result = {};

// Do the magic
result.rows = [...new Set(question.rows.map(row => row.no))].map(key => question.rows.filter(row => row.no === key));
  
console.log(result);

步骤-
1.创建一个唯一的no键数组,
2. 映射键,
3、从rows数组中过滤掉一个key相同(no)值的数组

【讨论】:

    【解决方案2】:

    您可以使用 Map 和其中的值,方法是使用键通过减少数组来创建相同的组。

    function groupBy(array, key) {
        return Array.from(array
            .reduce((m, o) => m.set(o[key], [...(m.get(o[key]) || []), o]), new Map)
            .values()
        );
    }
    
    let question = { rows: [{ no: 1, content: "a" }, { no: 2, content: "d" }, { no: 1, content: "b" }, { no: 3, content: "e" }, { no: 2, content: "c" }, { no: 4, content: "f" }, { no: 4, content: "g" }] },
        result = { rows: groupBy(question.rows, 'no') };
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      【解决方案3】:

      这称为解析,当您需要更改数据结构时。

      您无需任何花哨的方法即可完成。步骤:

      1. 创建一个唯一的数组,
      2. 如果子数组不存在,则创建子数组,
      3. 将元素添加到它们的子数组中

      let question = {
        rows: [
          { no: 1, content: "a" },
          { no: 2, content: "d" },
          { no: 1, content: "b" },
          { no: 3, content: "e" },
          { no: 2, content: "c" },
          { no: 4, content: "f" },
          { no: 4, content: "g" },
        ]
      };
      
      // Create a unique array
      let results = {
        rows: []
      };
      
      for (let key in question.rows) {
        var item = question.rows[key];
        var no = item.no - 1;
      
        // Create sub arrays if they don't already exist
        if (!results.rows[no]) results.rows[no] = [];
      
        // Add itens to their sub arrays
        results.rows[no].push(item);
      }
      
      console.log(results);

      【讨论】:

        猜你喜欢
        • 2022-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-04
        • 2017-03-30
        • 1970-01-01
        • 2011-12-31
        • 2013-08-10
        相关资源
        最近更新 更多