使用Javascript和loadash

时间:2017-04-21 20:48:43

标签: javascript sorting object lodash array-multisort

我正在尝试使用多个条件,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);

1 个答案:

答案 0 :(得分:0)

我们可以通过以下方式解决这个问题:

  1. 使用lodash#toPairs将对象转换为键值对的集合。
  2. 使用lodash#sortBy按键对集合进行排序:
    • 通过'_'分隔符拆分密钥字符串并获取length
    • 使用拆分的'_'替换密钥字符串中第一次出现的length字符。
  3. 使用lodash#fromPairs将已排序的集合转换为对象。
  4. var result = _(object)
      .toPairs()
      .sortBy(function(pair) {
        return pair[0].replace(/_/, pair[0].split('_').length);
      })
      .fromPairs()
      .value();
    

    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"
    };
    
    var result = _(object)
      .toPairs()
      .sortBy(function(pair) {
        return pair[0].replace(/_/, pair[0].split('_').length);
      })
      .fromPairs()
      .value();
      
    console.log(result);
    body > div { min-height: 100%; top: 0; }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

    或者,您可以在不使用lodash的情况下解决此问题:

    function getKey(key) {
      return key.replace(/_/, key.split('_').length);
    }
    
    var result = Object.keys(object)
      .sort(function(k1, k2) {
        return getKey(k1).localeCompare(getKey(k2));
      })
      .reduce(function(acc, key) {
        acc[key] = object[key];
        return acc;
      }, {});
    

    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"
    };
    
    function getKey(key) {
      return key.replace(/_/, key.split('_').length);
    }
    
    var result = Object.keys(object)
      .sort(function(k1, k2) {
        return getKey(k1).localeCompare(getKey(k2));
      })
      .reduce(function(acc, key) {
        acc[key] = object[key];
        return acc;
      }, {});
      
    console.log(result);
    body > div { min-height: 100%; top: 0; }