使用非数字键对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 theall
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 ...
-
使用dojo进行跨域请求(cross domain request with dojo)[2024-03-05]
我怀疑你在这里碰到了浏览器的安全性。 跨域请求仅在使用iframe或注入脚本(如您所做)以及该脚本的内容有效“text / javascript”时才有效。 如果您尝试将“text / html”加载到脚本中,它将无法正常工作,因为它不是有效的脚本。 这是我们大多数人在某些时候试图做的事情。 我花了好几个小时试图绕过跨域限制,发现安全阻止它是坚实的。 有关详细信息,请参阅我的答案 。 如果您要做的只是将内容加载到页面上,那么您可以使用 -
Spring dojo请求问题(Spring dojo request issue)[2021-08-23]
我发现了这个问题。 事实上有2个问题1.在Ajax调用中我必须有dojo.forEach(jsonData, ...)而不是dojo.forEach(jsonData.newsItems, ...) 2。在我的方法的控制器中我必须添加注解 public @ResponseBody MyObj 我希望这可以帮助某人解决同样的问题。 I found the issue. In fact there was 2 problems 1. In Ajax call i must have dojo.forEach ... -
我以某种方式得到了答案。 我只是在这里遵循codeproject教程的示例。 基本上,我使用了内存存储和缓存存储对象。 在我直接将json存储链接到对象存储之前。 SalesFormGridMemoryStore = new MemoryStore({ idProperty: "SalesNo" }); SalesFormGridJsonStore = new JsonRestStore({target:"/sales/SalesForm/DataRequestedDojoGrid/", idPropert ...
-
如果您想在左侧显示一个窗格,请使用'leading'作为区域的值然后在坎特区域,您将放置一个单独的边界容器,其中地图位于中心区域,而底部区域位于 ...
如何使用基本身份验证进行dojo.request.xhr GET请求(How to make dojo.request.xhr GET request with basic authentication)[2022-09-30]
实际上,您应该能够使用options对象中的user和password属性传递用户和密码。 在以前的Dojo版本中,这是有记录的,但似乎现在它们不是。 但是,我刚刚测试了它,它似乎在URL中添加了用户名和密码,例如: http://user:password@myUrl/example.json 通常,浏览器应该能够翻译此URL,以便设置请求标头。 您也可以手动设置这些标头,例如使用: xhr("example.json", { headers: { "Authorization" ...在url中,你正在获取file.json - 这似乎是错误的,真的应该是file.php吗? url: "file.php?path=&callback=?", 还要确保路径正确 - 您可以检查firebug中的Net选项卡以查看它尝试发送的请求 In the url, you are fetching file.json - that seems to be erroneous Should that really be file.php? url ...在你的情况下,“请求”不是“dojo”对象的属性。 这只是函数中传递的参数的名称。 因此,您可以将其分配给任何对象的任何属性,甚至是模块的属性。 所以你接近解决方案。 看看我的例子: define([ "dojo/_base/declare", ..... "dojo/request", "dojo/aspect" ], function ( declare, ..... request, aspect ) { return decl ...dojo/request/notify从未用于这些目的。 您从中获得的响应仅用于向您提供通知,它不会在请求中用于进一步处理,因此您的修改实际上会被忽略。 要拦截调用,您应该查看dojo/request/registry模块,您可以使用它来注册提供程序(可以用作拦截器)。 例如,如果要向请求添加数据,可以使用: Registry.register(function(url, options) { options.data.b = 5; return true; }, Request, true ...相关文章
更多- javascript 问题
- javascript常用对象
- javascript快速入门
- javascript的验证
- 请教javascript问题
- javascript 注释
- javascript 基本语法
- JavaScript 事件
- JavaScript 事件
- GitHub创建SSH Keys
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)