代码之家  ›  专栏  ›  技术社区  ›  jagmitg

对展平对象排序

  •  0
  • jagmitg  · 技术社区  · 6 年前

    编辑:

    var input = {
        "a11/a22/animations": {
            "title": "title here",
            "priority": 2
        },
        "a11/a22/colours": {
            "title": "title here",
            "priority": 1
        },
        "a11/a22/fonts": {
            "title": "title here",
            "priority": 3
        },
        "a11/a22/visibility": {
            "title": "title here",
            "priority": 4
        },
        "a11/b22/logo": {
            "title": "title here",
            "priority": 1
        },
        "a11/c22/define": {
            "title": "title here",
            "priority": 2
        },
        "a11/c22/ordered": {
            "title": "title here",
            "priority": 3
        },
        "a11/c22/unordered": {
            "title": "title here",
            "priority": 1
        },
        "a11/d22/foot": {
            "title": "title here",
            "priority": 2
        },
        "a11/d22/head": {
            "title": "title here",
            "priority": 1
        },
        "a11/e22/blockquote": {
            "title": "title here",
            "priority": 2
        },
        "a11/e22/headings": {
            "title": "title here",
            "priority": 1
        },
        "a11/e22/hr": {
            "title": "title here",
            "priority": 4
        },
        "a11/e22/inline-elements": {
            "title": "title here",
            "priority": 3
        },
        "a11/e22/paragraph": {
            "title": "title here",
            "priority": 6
        },
        "a11/e22/preformatted": {
            "title": "title here",
            "priority": 5
        },
        "a11/e22/time": {
            "title": "title here",
            "priority": 7
        },
        "b11/f22/menu": {
            "title": "title here",
            "priority": 1
        },
        "b11/g22/product-item": {
            "title": "title here",
            "priority": 1
        },
        "b11/h22/search": {
            "title": "title here",
            "priority": 1
        },
        "b11/i22/sub-menu": {
            "title": "title here",
            "priority": 1
        },
        "c11/j22/footer": {
            "title": "title here",
            "priority": 1
        },
        "c11/j22/title": {
            "title": "title here",
            "priority": 2
        },
        "c11/k22/header": {
            "title": "title here",
            "priority": 1
        }
      },
      output = {};
    
    Object.entries(input).forEach(
      ([k, v]) =>
        (k.split("/").reduce((o, k) => (o[k] = o[k] || {}), output).value = v)
    );
    
    console.log(output);
    
    console.log(output);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    这会使整个过程变平。然而,在 json 结构下的值,我有一个值叫做 priority 一个整数。我希望能够按 优先 在第二层。 a22 c22 ,它应该排序 define ordered 基于它下面的优先级。

    .sort((a, b) => input[a].priority - input[b].priority)
    

    预期结果 :

    {
      "a11": {
        "a22": {
          "colours": {
            "value": {
              "title": "title here",
              "priority": 1
            }
          },        
          "animations": {
            "value": {
              "title": "title here",
              "priority": 2
            }
          },
          "fonts": {
            "value": {
              "title": "title here",
              "priority": 3
            }
          },
          "visibility": {
            "value": {
              "title": "title here",
              "priority": 4
            }
          }
        },
        "b22": {
          "logo": {
            "value": {
              "title": "title here",
              "priority": 1
            }
          }
        },
        "c22": {
          "unordered": {
            "value": {
              "title": "title here",
              "priority": 1
            }
          },        
          "define": {
            "value": {
              "title": "title here",
              "priority": 2
            }
          },
          "ordered": {
            "value": {
              "title": "title here",
              "priority": 3
            }
          },
        },
        "d22": {
          "head": {
            "value": {
              "title": "title here",
              "priority": 1
            }
          },        
          "foot": {
            "value": {
              "title": "title here",
              "priority": 2
            }
          },
        },
        "e22": {
          "headings": {
            "value": {
              "title": "title here",
              "priority": 1
            }
          },        
          "blockquote": {
            "value": {
              "title": "title here",
              "priority": 2
            }
          },
          "inline-elements": {
            "value": {
              "title": "title here",
              "priority": 3
            }
          },      
          "hr": {
            "value": {
              "title": "title here",
              "priority": 4
            }
          },
          "preformatted": {
            "value": {
              "title": "title here",
              "priority": 5
            }
          },      
          "paragraph": {
            "value": {
              "title": "title here",
              "priority": 6
            }
          },
          "time": {
            "value": {
              "title": "title here",
              "priority": 7
            }
          }
        }
      },
      "b11": {
        "f22": {
          "menu": {
            "value": {
              "title": "title here",
              "priority": 1
            }
          }
        },
        "g22": {
          "product-item": {
            "value": {
              "title": "title here",
              "priority": 1
            }
          }
        },
        "h22": {
          "search": {
            "value": {
              "title": "title here",
              "priority": 1
            }
          }
        },
        "i22": {
          "sub-menu": {
            "value": {
              "title": "title here",
              "priority": 1
            }
          }
        }
      },
      "c11": {
        "j22": {
          "footer": {
            "value": {
              "title": "title here",
              "priority": 1
            }
          },
          "title": {
            "value": {
              "title": "title here",
              "priority": 2
            }
          }
        },
        "k22": {
          "header": {
            "value": {
              "title": "title here",
              "priority": 1
            }
          }
        }
      }
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Nina Scholz    6 年前

    在生成新对象之前,可以通过 priority

    var input = { "a11/a22/animations": { title: "title here", priority: 2 }, "a11/a22/colours": { title: "title here", priority: 1 }, "a11/a22/fonts": { title: "title here", priority: 3 }, "a11/a22/visibility": { title: "title here", priority: 4 }, "a11/b22/logo": { title: "title here", priority: 1 }, "a11/c22/define": { title: "title here", priority: 2 }, "a11/c22/ordered": { title: "title here", priority: 3 }, "a11/c22/unordered": { title: "title here", priority: 1 }, "a11/d22/foot": { title: "title here", priority: 2 }, "a11/d22/head": { title: "title here", priority: 1 }, "a11/e22/blockquote": { title: "title here", priority: 2 }, "a11/e22/headings": { title: "title here", priority: 1 }, "a11/e22/hr": { title: "title here", priority: 4 }, "a11/e22/inline-elements": { title: "title here", priority: 3 }, "a11/e22/paragraph": { title: "title here", priority: 6 }, "a11/e22/preformatted": { title: "title here", priority: 5 }, "a11/e22/time": { title: "title here", priority: 7 }, "b11/f22/menu": { title: "title here", priority: 1 }, "b11/g22/product-item": { title: "title here", priority: 1 }, "b11/h22/search": { title: "title here", priority: 1 }, "b11/i22/sub-menu": { title: "title here", priority: 1 }, "c11/j22/footer": { title: "title here", priority: 1 }, "c11/j22/title": { title: "title here", priority: 2 }, "c11/k22/header": { title: "title here", priority: 1 } },
        output = {};
    
    Object
        .entries(input)
        .sort(({ 1: { priority: a } }, { 1: { priority: b } }) => a - b)
        .forEach(([k, v]) => (k.split("/").reduce((o, k) => (o[k] = o[k] || {}), output).value = v)
    );
    
    console.log(output);
    .as-console-wrapper { max-height: 100% !important; top: 0; }