Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带索引和值的jQuery$.map_Jquery_Arrays_Json_Key - Fatal编程技术网

带索引和值的jQuery$.map

带索引和值的jQuery$.map,jquery,arrays,json,key,Jquery,Arrays,Json,Key,目前,我将JSON文件映射如下: var sortedData = $.map(response.data, function(data, index) { return [data]; }); sortedData.sort(function(a, b) { return (b[3] < a[3]) ? -1 : 1; }); "10": [ "Title 1", "Description 1" ], "12": [ "Title 2",

目前,我将JSON文件映射如下:

var sortedData = $.map(response.data, function(data, index) {
    return [data];
});
sortedData.sort(function(a, b) {
    return (b[3] < a[3]) ? -1 : 1;
});
"10": [
    "Title 1",
    "Description 1"
],
"12": [
    "Title 2",
    "Description 2"
]
这个很好用。但是缺少键(索引)。我如何添加它

JSON文件的内容:

{
    "index": 5,
    "timestamp": 1570438008,
    "data": {
        "12": [
            "Title 2",
            "Description 2"
        ],
        "10": [
            "Title 1",
            "Description 1"
        ]
    }
}
在$.map之后,我想对它进行排序(数据属性)。我是这样做的:

var sortedData = $.map(response.data, function(data, index) {
    return [data];
});
sortedData.sort(function(a, b) {
    return (b[3] < a[3]) ? -1 : 1;
});
"10": [
    "Title 1",
    "Description 1"
],
"12": [
    "Title 2",
    "Description 2"
]
。。不是这样的:

var sortedData = $.map(response.data, function(data, index) {
    return [data];
});
sortedData.sort(function(a, b) {
    return (b[3] < a[3]) ? -1 : 1;
});
"10": [
    "Title 1",
    "Description 1"
],
"12": [
    "Title 2",
    "Description 2"
]

您正在返回
[数据]
,它返回键
数据的值
,而应该只返回
数据
,它将返回包括索引在内的所有键的值

var jsonContent={
“指数”:5,
“时间戳”:157043808,
“数据”:{
"12": [
“标题2”,
“说明2”
],
"10": [
“标题1”,
“说明1”
]
}
};
var sortedTicketsDesc=$.map(jsonContent,function(数据,索引){
返回数据;
});
控制台日志(已分类的ticketsdesc)

它成功了,但我现在无法对内容(数据)进行排序。我使用sortedTicketsDesc.sort(函数(a,b){return(b[3]