整理展平的对象

时间:2018-09-30 19:49:43

标签: javascript sorting object

我当前有一个对象,并且想对其进行展平,下面的代码执行以下操作;

编辑:

只需更新结构即可包含优先级。

<item name="colorAccent">@color/colorAccent</item>
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);

这简化了整个过程。但是,在value下的.as-console-wrapper { max-height: 100% !important; top: 0; }结构中,我有一个名为json的整数。我希望能够在第二级按priority进行排序。 prioritya22,应根据其下面的优先级对c22defineordered进行排序。

ordered

预期结果

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

1 个答案:

答案 0 :(得分:1)

您可以通过生成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; }