简体   繁体   English

使用嵌入键按特定数值对 JSON 进行排序

[英]Sort JSON by specific numeric value with embedded keys

I am trying to sort through some JSON by highest amount to lowest with some nested values.我试图通过一些嵌套值从最高到最低对一些 JSON 进行排序。 Take the following object:取以下object:

"videos": {
    "0": {
      "video_name": "Calls",
      "video_filename": "ALEXA_CALLS.mp4",
      "count": 1
    },
    "1": {
      "video_name": "Routines",
      "video_filename": "ROUTINES.mp4",
      "count": 3
    },
    "4": {
      "video_name": "Photos",
      "video_filename": "PHOTOS.mp4",
      "count": 4
    }
  }

Is it possible to sort through this based on the 'count value', so the Photos would be the first property, then Routines, etc. Sort of like this:是否可以根据“计数值”对其进行排序,因此照片将是第一个属性,然后是例程等。有点像这样:

"videos": {       
    "4": {
      "video_name": "Photos",
      "video_filename": "PHOTOS.mp4",
      "count": 4
    },
    "1": {
      "video_name": "Routines",
      "video_filename": "ROUTINES.mp4",
      "count": 3
    },
    "0": {
      "video_name": "Calls",
      "video_filename": "ALEXA_CALLS.mp4",
      "count": 1
    },
  }

I've seen from other questions that you can sort if you convert the json object into an array and do something with.sort, but it seems the numbers "0", "1", "4", etc can cause issues as I can't figure out how to generically reference the whole object.我从其他问题中看到,如果将 json object 转换为数组并使用.sort 执行某些操作,则可以排序,但似乎数字“0”、“1”、“4”等可能会导致问题,因为我无法弄清楚如何通用引用整个 object。 With other stuff I am doing, I need to do stuff like videos[0].video_name.对于我正在做的其他事情,我需要做一些类似视频[0].video_name 的事情。

Anyone seen something like this before?以前有人见过这样的东西吗? Any tips would be great !任何提示都会很棒!

Update with example:更新示例:

let v = {
"0": {
  "video_name": "Calls",
  "video_filename": "ALEXA_CALLS.mp4",
  "count": 1
},
"1": {
  "video_name": "Routines",
  "video_filename": "ROUTINES.mp4",
  "count": 3
},
"4": {
  "video_name": "Photos",
  "video_filename": "PHOTOS.mp4",
  "count": 4
}
};

v.sort(function(a, b) {
return a.count > b.count;
});

console.log(v);

You can do it by converting it to an array您可以通过将其转换为数组来完成

 const obj = { "videos": { "0": { "video_name": "Calls", "video_filename": "ALEXA_CALLS.mp4", "count": 1 }, "1": { "video_name": "Routines", "video_filename": "ROUTINES.mp4", "count": 3 }, "4": { "video_name": "Photos", "video_filename": "PHOTOS.mp4", "count": 4 } } } const videos = obj.videos const videoArray = Object.entries(videos).map(([key, value]) => ({...value, key: key})) const sortedVideoArray = videoArray.sort((a, b) => b.count - a.count) console.log(sortedVideoArray)

there is no point in putting the values from the array back into an object since js objects do not guarantee that they will maintain insert order but that code snippet above does allow you to maintain which video had which key in the object将数组中的值放回 object 是没有意义的,因为 js 对象不保证它们会保持插入顺序,但上面的代码片段确实允许您维护 object 中哪个视频具有哪个键

First your data isn't quite JSON;首先,您的数据不完全是 JSON; it should be wrapped in another set of brackets like它应该被包裹在另一组括号中,例如

   {
    "videos": {       
        "4": {
          "video_name": "Photos",
          "video_filename": "PHOTOS.mp4",
          "count": 4
        },
        "1": {
          "video_name": "Routines",
          "video_filename": "ROUTINES.mp4",
          "count": 3
        },
        "0": {
          "video_name": "Calls",
          "video_filename": "ALEXA_CALLS.mp4",
          "count": 1
        }
      }
    }

From there you can parse it, grab out the values and sort via从那里你可以解析它,取出值并通过排序

const data = JSON.parse(...)

Object.values(data.videos).sort((a,b) => a.count - b.count)

Also, you can reverse the sort by reversing the body of the sort function此外,您可以通过反转排序 function 的主体来反转排序

(a,b) => b.count - a.count

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM