lz4*_*430 1 javascript arrays jquery object
我想做一些相对简单的事情,无论如何我想.
我需要将页面的路径名与对象的kv对进行比较.例如:
if("pathname" === "key"){return value;}
Run Code Online (Sandbox Code Playgroud)
这就是它.我不知道如何在常规Javascript或jQuery中执行此操作.要么是可以接受的.
你可以在这里看到我的小提琴:http://jsfiddle.net/lz430/2rhds1x3/
JavaScript的:
var pageID = "/electrical-electronic-tape/c/864";
var pageList = [{
"/electrical-electronic-tape/c/864": "ElectronicTape",
"/industrial-tape/c/889": "IndustrialTape",
"/sandblasting-tape/c/900": "SandblastingTape",
"/Foam-Tape/c/875": "FoamTape",
"/double-coated-d-c-dhesive-tape/c/872": "DCTape",
"/Adhesive-Transfer-Tape/c/919": "ATTape",
"/Reflective-Tape/c/884": "ReflectiveTape",
"/custom-moulding": "CustomMoulding",
"/request-a-quote": "RequestQuote"
}];
var label = pageID in pageList;
$('.el').html(label);
Run Code Online (Sandbox Code Playgroud)
首先,您的"pageList"应该只是一个普通对象,而不是数组中的对象:
var pageList = {
"/electrical-electronic-tape/c/864": "ElectronicTape",
"/industrial-tape/c/889": "IndustrialTape",
"/sandblasting-tape/c/900": "SandblastingTape",
"/Foam-Tape/c/875": "FoamTape",
"/double-coated-d-c-dhesive-tape/c/872": "DCTape",
"/Adhesive-Transfer-Tape/c/919": "ATTape",
"/Reflective-Tape/c/884": "ReflectiveTape",
"/custom-moulding": "CustomMoulding",
"/request-a-quote": "RequestQuote"
};
Run Code Online (Sandbox Code Playgroud)
然后,您可以将"label"设置为映射中的值:
var label = pageList[pageID] || "(not found)";
Run Code Online (Sandbox Code Playgroud)
如果查找失败,则上述语句的最后一位将标签设置为"(未找到)",这可能适用于您的情况,也可能不适用.