按JS中的值对嵌套字典进行排序

时间:2018-11-11 12:13:13

标签: javascript dictionary

有人可以帮忙找出用JavaScript里面的另一个字典对字典进行排序的方法吗? 这是我的字典的样子:

{'POS2': {'stegano': 0, 'sum': 200, 'misc': 100, 'web': 0, 'ppc': 0, 'crypto': 0, 'admin': 0, 'vuln': 0, 'forensics': 0, 'hardware': 0, 'reverse': 0, 'recon': 100}, ...}

我想通过存储在嵌套字典中的'sum'键对其进行排序。 我可以找到一些用python编写的解决方案,但是这里的挑战是要在JS中实现相同的功能。 不胜感激。

3 个答案:

答案 0 :(得分:0)

例如,您拥有下一个数组(类似于您的数组,但是具有较少的可读属性):

let obj = {
    pos1: { sum: 100, name: 'p1' },
    pos2: { sum: 1, name: 'p2' },
    pos3: { sum: 50, name: 'p3' }
};

var result = Object.keys(obj)
    .map(key => { return {key, val: obj[key]}}) // output: unsorted array
    .sort((a, b) => a.val.sum > b.val.sum); 

console.log(result);

结果,您将看到:

[ { key: 'pos2', val: { sum: 1, name: 'p2' } },
  { key: 'pos3', val: { sum: 50, name: 'p3' } },
  { key: 'pos1', val: { sum: 100, name: 'p1' } } ]

答案 1 :(得分:0)

只需使用Object.values检索内部对象,然后使用Array.sort对其内容进行排序。请注意,您无法对对象属性进行排序,因为它们默认情况下是未排序的集合(或者至少没有正确描述应如何对对象的属性进行排序)

const dict = {
  'POS2': {
    'stegano': 0, 
    'sum': 200, 
    'misc': 100, 
    'web': 0, 
    'ppc': 0, 
    'crypto': 0, 
    'admin': 0, 
    'vuln': 0, 
    'forensics': 0, 
    'hardware': 0, 
    'reverse': 0, 
    'recon': 100
  }, 
  'POS1': {
    'sum': 100
  },
  'POS3': {
    'sum': 250
  }
};

function orderBySubKey( input, key ) {
  return Object.values( input ).map( value => value ).sort( (a, b) => a[key] - b[key] );
}

console.log( orderBySubKey( dict, 'sum' ) );

此示例将删除外部属性,如果您要保留这些属性,则可以稍微更改顺序,例如

const dict = {
  'POS2': {
    'stegano': 0, 
    'sum': 200, 
    'misc': 100, 
    'web': 0, 
    'ppc': 0, 
    'crypto': 0, 
    'admin': 0, 
    'vuln': 0, 
    'forensics': 0, 
    'hardware': 0, 
    'reverse': 0, 
    'recon': 100
  }, 
  'POS1': {
    'sum': 100
  },
  'POS3': {
    'sum': 250
  }
};

function orderBySubKey( input, key ) {
  return Object.keys( input ).map( key => ({ key, value: input[key] }) ).sort( (a, b) => a.value[key] - b.value[key] );
}

console.log( orderBySubKey( dict, 'sum' ) );

这会给您带来不同的输出,但仍然可以让您在原始字典中查找

答案 2 :(得分:0)

由于无法按属性对对象进行排序,因此需要将对象map放入数组,然后对其进行排序。而且,您可能不想丢失每个嵌套对象(例如POS2)的密钥,因此您希望将该密钥嵌入对象中以便安全保存。

const obj = {"POS2":{"stegano":0,"sum":2200,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100},"POS3":{"stegano":0,"sum":1200,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100},"POS4":{"stegano":0,"sum":22,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100},"POS5":{"stegano":0,"sum":2001,"misc":100,"web":0,"ppc":0,"crypto":0,"admin":0,"vuln":0,"forensics":0,"hardware":0,"reverse":0,"recon":100}}

// map over the object keys
const out = Object.keys(obj).map(key => {

  // on each iteration return the object with its new key property
  return { ...obj[key], key }

// then sort the array of object by their sums
}).sort((a, b) => a.sum - b.sum);

console.log(out);