javascript - 将 JSON 对象转换为数组以在不丢失键/索引的情况下进行排序

标签 javascript arrays json sorting object

我很抱歉有类似的线程,但我无法解决这个问题。我有一个包含键和值(代表文件 ID 和文件名)的 JSON 对象。由于 JS 对象无法排序,我需要转换为数组,按值(即文件名)而不是键(即文件 ID)排序。我可以完成所有这一切,除了在转换为数组时,我丢失了我的键/文件 ID(例如 110、111、112)并且它们被默认数组键(0、1、2 等)替换。

// Assign object to a var
$obj_win_top_get_files = window.top.<?php echo $_GET['files']; ?>;

通过console.log查看对象,

console.log('checked_boxes', $obj_win_top_get_files);

我明白了:

对象{110:“013_904_general.docx”,111:“013_902_info.docx”,112:“013_120_list.docx”

// Sort JSON object by file name ("value") rather than by id ("key")
// Create an array first since JS object is NOT sortable
arr_ids_filenames = new Array(); 

// Loop thru JS object to populate new array so it can be subsequently sorted
$.each($obj_win_top_get_files, function (key, value) {
    console.log(key, value); 
    // Populate array
    arr_ids_filenames[key] = value;
});

// Sort array by values
arr_ids_filenames.sort();
// THIS IS WHERE I AM LOSING THE FILE IDs (keys)
$.each(arr_ids_filenames, function (key, value) {
    // Array may contain keys/IDs with no values/file names so make sure there is a value
    if(value){
        console.log(key, value); 
        $the_ul.append('<li id="chk_file_' + key + '">' + value + '</li>');
    }
});

一切正常,除了它们的键不是文件 ID (110,111,112),它们是默认的数组键 (0,1,2)。我在 $.each() 中做的不正确。我很接近,但无法解决这个问题。任何建议将不胜感激。

最佳答案

创建一个包含数组中 2 个项目的数组。键和对象。现在你有一个像这样的数组:

[
  [originalKey, obj],
  [originalKey, obj],
  [originalKey, obj]
]

然后使用自定义排序函数:

arr_ids_filenames.sort(function(a,b){ return a[1] - b[1] });

(注意:我假设你的对象只是整数,如果不是为了排序而这样做的话)

arr_ids_filenames.sort(function(a, b) {
  a = a[1]; // now a points to object 1
  b = b[1]; // now b points to object 2
  if (a is less than b by some ordering criterion) {
    return -1;
  }
  if (a is greater than b by the ordering criterion) {
    return 1;
  }
  // a must be equal to b
  return 0;
});

一起:

// Sort JSON object by file name ("value") rather than by id ("key")
// Create an array first since JS object is NOT sortable
arr_ids_filenames = new Array(); 

// Loop thru JS object to populate new array so it can be subsequently sorted
var i = 0;
$.each($obj_win_top_get_files, function (key, value) {
    console.log(key, value); 
    // Populate array
    arr_ids_filenames[i++] = [key,value];
});

// Sort array by values
arr_ids_filenames.sort(function(a,b){ return a[1] - b[1] });
// THIS IS WHERE I AM LOSING THE FILE IDs (keys)
$.each(arr_ids_filenames, function (i, v) {
    // Array may contain keys/IDs with no values/file names so make sure there is a value
    if(v[0]){
        var key = v[0];
        var value = v[1];
        $the_ul.append('<li id="chk_file_' + key + '">' + value + '</li>');
    }
});

关于javascript - 将 JSON 对象转换为数组以在不丢失键/索引的情况下进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33791754/

相关文章:

javascript - 如何使用 transform scale 属性在不改变边框的情况下分解盒子的内容?

javascript - react : How to pass html as prop

javascript - 如何设置函数的原型(prototype)

java - 从一个类到另一个类获取数组长度

c - C语言二维数组

arrays - 将命令的输出与 bash 中的数组相匹配

javascript - 如何从这个 JSON Javascript 打印数据?

php - 打印出包含多个数组的 JSON

javascript - Emacs命令将JS对象转换为JSON

javascript - 在 unity C# 中手动将值分配给数组