更改输出';用Javascript实现s值
我有一个颜色改变工具,输出十六进制值。我需要的是将结束形式上的值更改为颜色名称。有几个输出需要更改,颜色名称列表有1512个条目。例如,我的一个输出是cp-swbluesbg-output,如果它返回一个值#006a66,那么它应该被转换为“Poseiden” 输出字符串为:更改输出';用Javascript实现s值,javascript,jquery,outputstream,Javascript,Jquery,Outputstream,我有一个颜色改变工具,输出十六进制值。我需要的是将结束形式上的值更改为颜色名称。有几个输出需要更改,颜色名称列表有1512个条目。例如,我的一个输出是cp-swbluesbg-output,如果它返回一个值#006a66,那么它应该被转换为“Poseiden” 输出字符串为: function(event, color) { $('.cp-swbluesbbg-output').text(color.formatted); /*returns the value i need t
function(event, color) {
$('.cp-swbluesbbg-output').text(color.formatted);
/*returns the value i need to convert. i.e. #006a66*/
我的var设置如下(固定为一种更简单的格式,共有1512个条目):
我已经尝试了正常字符串搜索的多次迭代,但似乎都不起作用。
我对这一点非常陌生,很难找到一个搜索,根据每种颜色的输出返回颜色名称。非常感谢您的帮助。
newName
看起来像一个json数组。。由于您使用的是jquery,因此在获取颜色作为参数的函数中,您可以使用newName[color]
查找颜色名称,其中color
是十六进制代码。使用当前设置,获取值的最快方法是在整个对象中循环,直到找到所需的值,然后中断。大概是这样的:
for (var currentColorPair in newName) {
if (newName.hasOwnProperty(currentColorPair)) {
if (newName[currentColorPair][hexValue]) {
return newName[currentColorPair][hexValue];
}
}
}
return "Color does not exist";
var newName = {
"#006A66" : "Poseidon SW6762",
"#006A84" : "Blue Nile SW6776",
"#006B55" : "Starboard SW6755",
"#006E7F" : "Maxi Teal SW6769"};
if (newName[hexValue]) {
return newName[hexValue];
}
else {
return "Color does not exist";
}
return (newName[hexValue]) ? newName[hexValue] : "Color does not exist";
但是,如果可以将“newName”的结构更新为更简单的数组,如下所示:
for (var currentColorPair in newName) {
if (newName.hasOwnProperty(currentColorPair)) {
if (newName[currentColorPair][hexValue]) {
return newName[currentColorPair][hexValue];
}
}
}
return "Color does not exist";
var newName = {
"#006A66" : "Poseidon SW6762",
"#006A84" : "Blue Nile SW6776",
"#006B55" : "Starboard SW6755",
"#006E7F" : "Maxi Teal SW6769"};
if (newName[hexValue]) {
return newName[hexValue];
}
else {
return "Color does not exist";
}
return (newName[hexValue]) ? newName[hexValue] : "Color does not exist";
然后你可以简单地这样称呼它:
for (var currentColorPair in newName) {
if (newName.hasOwnProperty(currentColorPair)) {
if (newName[currentColorPair][hexValue]) {
return newName[currentColorPair][hexValue];
}
}
}
return "Color does not exist";
var newName = {
"#006A66" : "Poseidon SW6762",
"#006A84" : "Blue Nile SW6776",
"#006B55" : "Starboard SW6755",
"#006E7F" : "Maxi Teal SW6769"};
if (newName[hexValue]) {
return newName[hexValue];
}
else {
return "Color does not exist";
}
return (newName[hexValue]) ? newName[hexValue] : "Color does not exist";
。或者,更简单地说,像这样:
for (var currentColorPair in newName) {
if (newName.hasOwnProperty(currentColorPair)) {
if (newName[currentColorPair][hexValue]) {
return newName[currentColorPair][hexValue];
}
}
}
return "Color does not exist";
var newName = {
"#006A66" : "Poseidon SW6762",
"#006A84" : "Blue Nile SW6776",
"#006B55" : "Starboard SW6755",
"#006E7F" : "Maxi Teal SW6769"};
if (newName[hexValue]) {
return newName[hexValue];
}
else {
return "Color does not exist";
}
return (newName[hexValue]) ? newName[hexValue] : "Color does not exist";
如果在某个地方执行
console.log(newName[“#006A66”])
会发生什么?将十六进制代码作为键传入,然后将文本设置为与该键关联的值,这可能很简单。您可以更改颜色对象的格式吗?有这么多的微小阵列在其中,这使得过程更长。假设颜色值是唯一的,您应该能够如下格式化:var newName={“#006A66”:“波塞冬SW6762”,“#006A84”:“蓝尼罗河SW6776”,“#006B55”:“右舷SW6755”,“#006E7F”:“Maxi Teal SW6769”}
,这将使您的搜索更加简单。@talemyn-我可以轻松更改“newName”的格式,所以这不会是什么大问题,只是它太大了。你有没有一个从字符串中获取所需值的好方法的建议?补充回答。更改格式使解决方案更简单。对于未更改的“newName”方法,我的解决方案中有一个小错误。刚刚解决了这个问题。“改变格式”的解决方案应该还可以。这看起来很简单,但我无法让它工作。我已经重置了newName
var
的结构,并将hexValue
的var
设置为document.getElementsByClassName(“cp Bluesbg输出”).value代码>但它不返回任何内容。我注意到在示例代码中,您将\006a66
列为要返回的值,而数组中的值是\006a66
。在查找值时大小写很重要,因此,由于所有数组数据都是大写的,请尝试将代码更新为document.getElementsByClassName(“cp-bluesbg-output”).value.toUpperCase()
并查看这是否有帮助。