javascript:更改所有键的值顺序

时间:2018-11-05 23:15:21

标签: javascript jquery arrays sorting object

我有一个具有以下结构的对象:

object = { ids: [], labels:[], values:[] }

每个键都有80个值。我想按降序对“值”键进行排序(从大到小),并相应地更新ID和标签键(因此,最高“值”值的标签和ID现在是第一个)。我尝试了以下代码,但没有用:

function getSorted(arr, sortArr) {
  var result = [];
  for(var i=0; i<arr.length; i++) {
    console.log(sortArr[i], arr[i]);
    result[i] = arr[sortArr[i]];
  }
  return result;
}

我要排序的数据是这样的:

ids = [41, 121, 159, 165, 170, 189, 258, 259, 307, 340, 342, 352, 357, 412, 482, 513, 725, 830, 833, 874, 907, 944, 1167, 1169, 1189, 1193, 1208, 1232, 1274, 1314, 1497, 1498, 1503, 1505, 1795, 1950, 1959, 1960, 1962, 1968, 1977, 2011, 2024, 2039, 2065, 2077, 2110, 2167, 2178, 2184, 2186, 2188, 2191, 2235, 2244, 2247, 2264, 2275, 2291, 2318, 2335, 2342, 2350, 2396, 2419, 2475, 2483, 2491, 2546, 2571, 2722, 2737, 2739, 2782, 2811, 2859, 2908, 2936, 2964, 3450]

values = [71, 2, 2, 12, 2, 47, 2, 2, 7, 2, 2, 50, 2, 2, 113, 2, 2, 10, 3, 36, 3, 19, 163, 2, 51, 6, 5, 2, 4, 3, 2, 2, 2, 2, 10, 25, 30, 2, 3, 2, 40, 11, 13, 5, 2, 23, 2, 5, 7, 19, 3, 2, 28, 2, 14, 11, 78, 22, 2, 40, 3, 2, 2, 11, 13, 2, 2, 2, 2, 2, 8, 4, 4, 12, 13, 126, 7, 3, 10, 37]

labels = ["Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria;Actinobacteria;Actinobacteria;Actinomycetales", "Bacteria;Actinobacteria;Actinobacteria;Actinomycetales", "Bacteria;Actinobacteria;Actinobacteria;Actinomycetales;Actinomycetaceae", "Bacteria;Actinobacteria;Actinobacteria;Actinomycetales;Actinomycetaceae;Varibaculum", "Bacteria;Actinobacteria;Actinobacteria;Actinomycetales;Corynebacteriaceae", "Bacteria;Actinobacteria;Actinobacteria;Actinomycetales;Corynebacteriaceae;Corynebacterium", "Bacteria;Bacteroidetes;Bacteroidia;Bacteroidales;Porphyromonadaceae;Porphyromonas", "Bacteria;Bacteroidetes;Bacteroidia;Bacteroidales;Porphyromonadaceae;Porphyromonas", "Bacteria;Bacteroidetes;Bacteroidia;Bacteroidales;Porphyromonadaceae;Porphyromonas", "Bacteria;Bacteroidetes;Bacteroidia;Bacteroidales;Porphyromonadaceae;Porphyromonas", "Bacteria;Bacteroidetes;Bacteroidia;Bacteroidales;Porphyromonadaceae;Porphyromonas", "Bacteria;Bacteroidetes;Bacteroidia;Bacteroidales;Porphyromonadaceae;Porphyromonas", "Bacteria;Bacteroidetes;Bacteroidia;Bacteroidales;Prevotellaceae", "Bacteria;Bacteroidetes;Bacteroidia;Bacteroidales;Prevotellaceae;Prevotella", "Bacteria;Firmicutes", "Bacteria;Firmicutes", "Bacteria;Firmicutes", "Bacteria;Firmicutes", "Bacteria;Firmicutes;Bacilli;Bacillales;Staphylococcaceae;Staphylococcus", "Bacteria;Firmicutes;Clostridia", "Bacteria;Firmicutes;Clostridia", "Bacteria;Firmicutes;Clostridia", "Bacteria;Firmicutes;Clostridia", "Bacteria;Firmicutes;Clostridia", "Bacteria;Firmicutes;Clostridia;Clostridiales", "Bacteria;Firmicutes;Clostridia;Clostridiales", "Bacteria;Firmicutes;Clostridia;Clostridiales", "Bacteria;Firmicutes;Clostridia;Clostridiales", "Bacteria;Firmicutes;Clostridia;Clostridiales", "Bacteria;Firmicutes;Clostridia;Clostridiales", "Bacteria;Firmicutes;Clostridia;Clostridiales", "Bacteria;Firmicutes;Clostridia;Clostridiales", "Bacteria;Firmicutes;Clostridia;Clostridiales", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Anaerococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Finegoldia", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Gallicola", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Gallicola", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Peptoniphilus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Peptoniphilus", "Bacteria;Firmicutes;Clostridia;Clostridiales;IncertaeSedisXI;Peptoniphilus", "Bacteria;Firmicutes;Clostridia;Clostridiales;Peptococcaceae;Peptococcus", "Bacteria;Firmicutes;Clostridia;Clostridiales;Ruminococcaceae", "Bacteria;Firmicutes;Clostridia;Clostridiales;Veillonellaceae", "Bacteria;Proteobacteria;Epsilonproteobacteria;Campylobacterales;Campylobacteraceae;Campylobacter"]

我应该如何解决这个问题?谢谢

2 个答案:

答案 0 :(得分:1)

这三组数据之间唯一的关系是每个数组内部的索引,排序后这些索引将丢失。为了使三组数据保持配对,我将在您的数据上映射并为每个数据创建一个对象:

const data = { ids, labels, values };

const list = data.values.map((value, idx) => {
  return {
    id: data.ids[idx],
    value,
    label: data.labels[idx]
  };
});

现在list包含一个对象数组:

[
  {
    id: 41,
    label: 'Bacteria',
    value: '71',
  },
  {
    // ... etc
  }
]

这是一种更容易使用的数据结构,因为您希望idlabelvalue保持配对。

到那时,您可以按照降序的value属性轻松地对对象数组进行排序,如下所示:

list.sort((a, b) => {
  return b.value - a.value;
});

现在list是数据对象的数组,按object.value降序排列。

您可以使用此对象数组,也可以轻松地将其转换回三个不同数组的原始格式。

答案 1 :(得分:0)

我知道这与您的要求有所不同,但是也许您可以对如何适应自己的需求有所了解。

之前,我做了此功能来解决对象的排序问题。

function sortObj(obj, index, sorting) {
  let ans = [];
  sorting = sorting.toUpperCase();

  if (obj.constructor === Array) {
    for (let i=0; i < obj.length; i++) {
      ans.push(obj[i]);
    }
  }
  else {
    for (let k in obj) {
      if (obj.hasOwnProperty(k)) {
        ans.push(obj[k])
      }
    }
  }

  if (!ans.length)
    return [];


  if (isNaN(ans[0][index])) {
    ans = ans.sort(function (a,b) {
      if (a[index]) {
        if (sorting === "ASC")
          return a[index].localeCompare(b[index]);
        else
          return b[index].localeCompare(a[index]);
      }
      return [];
    });
  }
  else {
    ans = ans.sort(function (a, b) {
      if (sorting === "ASC")
        return a[index] - b[index];

      else
        return b[index] - a[index];
    })
  }

  return ans;
}

// structure of object
obj = {"1": {id: "1", label: "A", value: "5"}, "2": {id: "2", label: "B", value: "1"}}


sorted = sortObj(obj, "label", "ASC");
// [{id: "1", label: "A", value: "5"}, {id: "2", label: "B", value: "1"}]


sorted = sortObj(obj, "value", "ASC");
// [{id: "2", label: "B", value: "1"}, {id: "1", label: "A", value: "5"}]