【发布时间】:2017-04-21 20:48:43
【问题描述】:
我正在尝试使用多个条件、Javascript 和Lodash 按键对对象进行排序。我不必使用 Lodash,但认为这会更容易。在这一点上,我对任何可行的方法都持开放态度。任何帮助都将非常感激!提前谢谢你。
这是我正在使用的示例对象:
var object = {
"148295945589000200_149099156236000910_filter": "some value",
"148295945589000200_149099156236000910_filtertype": "some_value",
"148295945589000200_filter": "asdf",
"149098531921500909_source": "js.content_ids",
"149098531921500909_filtertype": "some_value",
"148295944269000199_settovar": "",
"148295945589000200_149098530604100908_source": "js.content_type",
"148295945589000200_filtertype": "defined",
"148295945589000200_source": "js.content_type",
"148295944269000199_settotext": "adsf",
"148295945589000200_149098530604100908_filter": "some value",
"149099546962400202_149099548423700203_filtertype": "some_value",
"149099546962400202_149099548423700203_filter": "some value",
"148295945589000200_149098530604100908_filtertype": "some_value",
"148295944269000199_setoption": "text",
"148295944269000199_set": "js.content_name",
"149099546962400202_source": "js.acme_id",
"149099546962400202_149099548423700203_source": "js.acme_page",
"149099546962400202_filter": "",
"149098531921500909_filter": "some value",
"149099546962400202_filtertype": "defined",
"148295945589000200_149099156236000910_source": "js.content_name"
}
这就是我想要做的:
根据每个键中的前 18 个数字对对象进行排序。如果只有一个数字块,首先列出,然后按顺序列出具有第二个数字块的键。然后,按密钥的剩余字母部分排序。
预期输出:
var object = {
"148295944269000199_set": "js.content_name",
"148295944269000199_setoption": "text",
"148295944269000199_settotext": "adsf",
"148295944269000199_settovar": "",
"148295945589000200_filter": "asdf",
"148295945589000200_filtertype": "defined",
"148295945589000200_source": "js.content_type",
"148295945589000200_149098530604100908_filter": "some value",
"148295945589000200_149098530604100908_filtertype": "some_value",
"148295945589000200_149098530604100908_source": "js.content_type",
"148295945589000200_149099156236000910_filter": "some text",
"148295945589000200_149099156236000910_filtertype": "some_value",
"148295945589000200_149099156236000910_source": "js.content_name"
"149098531921500909_filter": "some value",
"149098531921500909_filtertype": "contains_ignore_case",
"149098531921500909_source": "js.content_ids",
"149099546962400202_filter": "",
"149099546962400202_filtertype": "defined",
"149099546962400202_source": "js.acme_page",
"149099546962400202_149099548423700203_filter": "some value",
"149099546962400202_149099548423700203_filtertype": "some_value",
"149099546962400202_149099548423700203_source": "js.acme_id"
}
以下是我尝试过的一些事情:
(1) 尝试添加到数组并排序
var array = Object.keys(object).sort(function(a, b) {
return a.localeCompare(b);
})
var newObject = {};
array.forEach(function(key) {
newObject[key] = object[key];
});
JSON.stringify(object, null, 2);
(2) 尝试去掉下划线进行排序
var newObject = {};
Object.keys(object).forEach(function(key) {
newKey = key.replace(/_/g, "");
newObject[newKey] = object[key];
});
var array = Object.keys(newObject).sort(function(a, b) {
return a.localeCompare(b);
})
var newestObject = {};
array.forEach(function(key) {
newestObject[key] = newObject[key];
});
JSON.stringify(object, null, 2);
(3) 尝试使用 lodash(用“_”调用)转换为多维数组并使用多标准排序 - 我想我可能是最接近这个的。这需要 lodash 才能工作 -https://raw.githubusercontent.com/lodash/lodash/4.17.4/dist/lodash.core.js
var sortable = _.toPairs(object);
var array = sortable.sort(function(a, b) {
// check if b[0].substring(19,37) is a number first?
if (a[0].substring(0, 18) === b[0].substring(0, 18)) {
var x = a[1].substring(0, 18),
y = b[1].substring(0, 18);
return x < y ? -1 : x > y ? 1 : 0;
}
return a[0].substring(0, 18) - b[0].substring(0, 18);
});
var output = _.fromPairs(array);
JSON.stringify(output, null, 2);
【问题讨论】:
-
排序顺序有什么规定?你弄乱了代码:(
-
您好 Aravind,感谢您的回复。我的代码 sn-ps 看起来不错,但也许它们没有正确呈现。这是我正在寻找的标准:根据每个键中的前 18 个数字对对象进行排序。如果只有一个数字块,首先列出,然后按顺序列出具有第二个数字块的键。然后,按键的剩余字母部分排序。
-
排序对象还是排序数组??你的样本只包含一个对象
-
对对象进行排序。我尝试的一些事情涉及将对象转换为数组格式,但目标是对对象进行排序。
-
服务是返回数据集还是每次都修改key?
标签: javascript sorting object lodash array-multisort