首页 \ 问答 \ 使用非数字键对javascript数组进行排序(Sort javascript array with non-numeric keys)

使用非数字键对javascript数组进行排序(Sort javascript array with non-numeric keys)

这是我的阵列:

var a = [];
a["P.M.L."] = 44;
a["P.CO."] = 56;
a["M.É.D."] = 10;

现在我正在尝试对数组进行排序,使其看起来像:

["M.É.D." : 10, "P.M.L." : 44, "P.CO." : 56]

我尝试了很多解决方案,但没有一个成功。 我想知道你们其中一个人是否知道如何对数组进行排序。


Here is my array :

var a = [];
a["P.M.L."] = 44;
a["P.CO."] = 56;
a["M.É.D."] = 10;

Now i am trying to sort the array so it looks like :

["M.É.D." : 10, "P.M.L." : 44, "P.CO." : 56]

I have tried many solutions and none of them have been successfull. I was wondering if one of you had any idea how to sort the array.


原文:https://stackoverflow.com/questions/21171650
更新时间:2022-07-17 15:07

最满意答案

您的两个请求都将返回给您一个承诺。 dojo/promise/all模块完全按照您的需要执行操作:等待两个Promise解析,然后您可以使用响应执行任何操作。 有关all模块的更多信息,请点击此处链接

在你的情况下,代码应该是这样的:

require(["dojo/promise/all", "dojo/request"], function(all, request) {
    var promiseA = request("file1.json", {handleAs: "json"}),
        promiseB = request("file2.json", {handleAs: "json"});

    all([promiseA, promiseB).then(function(results) {
        // Results is the array with the promises results.
        // results[0] will be the return from promiseA
        // results[1] will be the return from promiseB
    });
}

Both of your requests will return to you a Promise. The dojo/promise/all module do exactly what you need: wait for the two Promises to resolve and then you can do whatever you need with the responses. For more information about the all module, here's the link.

In you case, the code should be something like:

require(["dojo/promise/all", "dojo/request"], function(all, request) {
    var promiseA = request("file1.json", {handleAs: "json"}),
        promiseB = request("file2.json", {handleAs: "json"});

    all([promiseA, promiseB).then(function(results) {
        // Results is the array with the promises results.
        // results[0] will be the return from promiseA
        // results[1] will be the return from promiseB
    });
}

相关问答

更多
  • 您的两个请求都将返回给您一个承诺。 dojo/promise/all模块完全按照您的需要执行操作:等待两个Promise解析,然后您可以使用响应执行任何操作。 有关all模块的更多信息,请点击此处链接 。 在你的情况下,代码应该是这样的: require(["dojo/promise/all", "dojo/request"], function(all, request) { var promiseA = request("file1.json", {handleAs: "json"}), ...
  • 我已经将requireCacheUrl和require.cache值打印到dojo / text.js的load()方法中。 至少在我的情况下,缓存中的模板键与一个斜杠上的查找键不同。 例如,我在我的小部件中有"dojo/text!./templates/Address.html" 。 它在缓存中显示关键url:/app/view/templates/Address.html ,但会像url:app/view/templates/Address.html ,导致缓存未命中和xhr请求。 在dojo / te ...
  • 我怀疑你在这里碰到了浏览器的安全性。 跨域请求仅在使用iframe或注入脚本(如您所做)以及该脚本的内容有效“text / javascript”时才有效。 如果您尝试将“text / html”加载到脚本中,它将无法正常工作,因为它不是有效的脚本。 这是我们大多数人在某些时候试图做的事情。 我花了好几个小时试图绕过跨域限制,发现安全阻止它是坚实的。 有关详细信息,请参阅我的答案 。 如果您要做的只是将内容加载到页面上,那么您可以使用