打字稿:将字典转换成数组而不改变其顺序

时间:2019-06-25 10:29:15

标签: javascript typescript dictionary

我正在使用lodash将值数组分组。根据我的代码

_.groupBy(array, a => a.level)

它产生正确的顺序

 1.0: [{…}]
1.1: [{…}]
1.1.1: [{…}]
1.1.1.1: [{…}]
1.1.1.1.1: [{…}]
1.1.1.1.1.1: [{…}]
1.1.1.1.1.2: [{…}]
1.1.1.1.1.3: [{…}]
1.1.1.1.1.4: [{…}]
1.1.1.1.1.5: [{…}]
1.1.1.1.1.6: [{…}]
1.1.1.1.2: [{…}]
1.1.1.1.2.1: [{…}]
1.1.1.1.2.2: [{…}]
1.1.1.1.3: [{…}]
1.1.1.1.4: [{…}]
1.1.1.1.5: [{…}]
1.1.1.1.6: [{…}]
1.1.1.1.7: [{…}]
1.1.1.1.8: [{…}]
1.1.1.1.9: [{…}]
1.1.2: [{…}]
1.1.2.1: [{…}]
1.1.2.2: [{…}]
1.1.2.3: [{…}]
1.1.3: [{…}]
1.1.3.1: [{…}]
1.1.3.2: [{…}]
1.1.3.3: [{…}]
1.1.3.4: [{…}]
1.1.4: [{…}]
1.1.4.1: [{…}]
1.1.4.1.1: [{…}]
1.1.4.1.2: [{…}]
1.1.4.1.3: [{…}]
1.1.4.1.4: [{…}]
1.1.4.1.5: [{…}]
1.1.4.1.6: [{…}]
1.1.4.2: [{…}]
1.1.4.3: [{…}]
1.1.4.4: [{…}]
1.1.4.5: [{…}]
1.1.4.6: [{…}]
1.1.4.7: [{…}]
1.1.4.8: [{…}]
1.1.4.9: [{…}]
1.1.4.10: [{…}]
1.1.4.11: [{…}]
1.1.5: [{…}]
1.1.5.1: [{…}]
1.1.5.2: [{…}]
1.1.5.3: [{…}]
1.1.5.4: [{…}]
1.1.5.5: [{…}]
1.1.5.6: [{…}]
1.1.5.7: [{…}]
1.1.5.8: [{…}]
1.1.5.9: [{…}]
1.1.5.10: [{…}]
1.1.5.11: [{…}]
1.1.5.12: [{…}]
1.1.5.13: [{…}]
1.1.5.14: [{…}]
1.1.5.15: [{…}]
1.1.5.16: [{…}]
1.1.5.17: [{…}]
1.1.5.18: [{…}]
1.1.5.19: [{…}]
1.1.5.20: [{…}]
1.1.5.21: [{…}]
1.1.5.22: [{…}]
1.1.5.23: [{…}]
1.1.5.24: [{…}]
1.1.5.25: [{…}]
1.1.5.26: [{…}]
1.1.5.27: [{…}]
1.1.5.28: [{…}]
1.1.5.29: [{…}]
1.1.5.30: [{…}]
1.1.5.31: [{…}]
1.1.6: [{…}]
1.1.7: [{…}]
1.1.8: [{…}]
1.1.9: [{…}]
1.1.10: [{…}]
1.1.11: [{…}]
1.2: [{…}]
1.2.1: [{…}]
1.2.2: [{…}]
1.2.3: [{…}]
1.2.3.1: [{…}]
1.2.3.2: [{…}]
1.2.4: [{…}]
1.2.4.1: [{…}]
1.2.4.2: [{…}]
1.2.4.3: [{…}]
1.2.4.4: [{…}]
1.2.4.5: [{…}]
1.2.4.6: [{…}]
1.2.5: [{…}]
1.2.6: [{…}]
1.2.7: [{…}]
1.2.8: [{…}]
1.2.9: [{…}]
1.2.10: [{…}]
1.2.11: [{…}]
1.2.12: [{…}]
1.2.13: [{…}]
1.2.14: [{…}]
1.3: [{…}]
1.4: [{…}]
1.4.1: [{…}]
1.4.2: [{…}]
1.5: [{…}]

let grouped = _.groupBy(array, a => a.level) Object.keys(grouped)

如果我使用任何一种转换方法,字典的顺序都会改变。 主要目的是在转换时保留订单。 实际输出为

 0: "1.0"
 1: "1.1"
 2: "1.1.1"
 3: "1.1.1.1"
 4: "1.1.1.1.1"
 5: "1.1.1.1.2"
 6: "1.1.1.1.3"
 7: "1.1.2"
 8: "1.1.2.1"
 9: "1.1.3"
 10: "1.1.4"
 11: "1.1.4.1"
 12: "1.1.5"
 13: "1.2"
 14: "1.2.1"
 15: "1.2.2"
 16: "1.2.3"
 17: "1.2.3.1"
 18: "1.2.4"
 19: "1.2.5"
 20: "1.2.6"
 21: "1.2.7"
 22: "1.2.8"
 23: "1.3"
 24: "1.4"
 25: "1.4.1"
 26: "1.4.2"
 27: "1.5"
 28: "1.1.1.1.1.1"
 29: "1.1.1.1.1.2"
 30: "1.1.1.1.1.3"
 31: "1.1.1.1.1.4"
 32: "1.1.1.1.1.5"
 33: "1.1.1.1.1.6"
 34: "1.1.1.1.2.1"
 35: "1.1.1.1.2.2"
 36: "1.1.1.1.4"
 37: "1.1.1.1.5"
 38: "1.1.1.1.6"
 39: "1.1.1.1.7"
 40: "1.1.1.1.8"
 41: "1.1.1.1.9"
 42: "1.1.2.2"
 43: "1.1.2.3"
 44: "1.1.3.1"
 45: "1.1.3.2"
 46: "1.1.3.3"
 47: "1.1.3.4"
 48: "1.1.4.1.1"
 49: "1.1.4.1.2"
 50: "1.1.4.1.3"
 51: "1.1.4.1.4"
 52: "1.1.4.1.5"
 53: "1.1.4.1.6"
 54: "1.1.4.2"
 55: "1.1.4.3"
 56: "1.1.4.4"
 57: "1.1.4.5"
 58: "1.1.4.6"
 59: "1.1.4.7"
 60: "1.1.4.8"
 61: "1.1.4.9"
 62: "1.1.4.10"
 63: "1.1.4.11"
 64: "1.1.5.1"
 65: "1.1.5.2"
 66: "1.1.5.3"
 67: "1.1.5.4"
 68: "1.1.5.5"
 69: "1.1.5.6"
 70: "1.1.5.7"
 71: "1.1.5.8"
 72: "1.1.5.9"
 73: "1.1.5.10"
 74: "1.1.5.11"
 75: "1.1.5.12"
 76: "1.1.5.13"
 77: "1.1.5.14"
 78: "1.1.5.15"
 79: "1.1.5.16"
 80: "1.1.5.17"
 81: "1.1.5.18"
 82: "1.1.5.19"
 83: "1.1.5.20"
 84: "1.1.5.21"
 85: "1.1.5.22"
 86: "1.1.5.23"
 87: "1.1.5.24"
 88: "1.1.5.25"
 89: "1.1.5.26"
 90: "1.1.5.27"
 91: "1.1.5.28"
 92: "1.1.5.29"
 93: "1.1.5.30"
 94: "1.1.5.31"
 95: "1.1.6"
 96: "1.1.7"
 97: "1.1.8"
 98: "1.1.9"
 99: "1.1.10"
 [100 … 113]
 100: "1.1.11"
 101: "1.2.3.2"
 102: "1.2.4.1"
 103: "1.2.4.2"
 104: "1.2.4.3"
 105: "1.2.4.4"
 106: "1.2.4.5"
 107: "1.2.4.6"
 108: "1.2.9"
 109: "1.2.10"
 110: "1.2.11"
 111: "1.2.12"
 112: "1.2.13"
 113: "1.2.14"

。顺序已完全更改,我需要按照字典中的顺序进行操作。

0 个答案:

没有答案