使 JavaScript 代码变得干燥的最优雅的方法

发布于 2024-11-08 02:19:12 字数 537 浏览 1 评论 0原文

这是我想要干燥的 JavaScript 代码片段:

if(agency == 'abcd')map.entities.push(abcd);
if(agency == 'efgh')map.entities.push(efgh);
if(agency == 'xyz')map.entities.push(xyz);
if(agency == 'pqrs') map.entities.push(pqrs);
if(agency == 'values')map.entities.push(values);

现在将来可能会有更多针对不同键的 if。JavaScript 不提供我可以在此处使用的 HashMap 构建。使用数组和 id 之类的东西来使其干燥也太俗气了。 有没有更简单的解决方案?可能是这样的

if(agency == 'abcd')map.entities.push(stringToVariable('abcd'));

,然后我可以使用 for 并遍历键。我不确定这在 JavaScript 中是否可能。

Here is a snippet of my JavaScript Code that I want to DRY up:

if(agency == 'abcd')map.entities.push(abcd);
if(agency == 'efgh')map.entities.push(efgh);
if(agency == 'xyz')map.entities.push(xyz);
if(agency == 'pqrs') map.entities.push(pqrs);
if(agency == 'values')map.entities.push(values);

Now in future may have more ifs for different keys coming in. JavaScript doesnt offer a build in HashMap which i can use here. And using arrays and id stuff to make it DRY is too tacky.
Is there a more simpler solution to this? May be something like this

if(agency == 'abcd')map.entities.push(stringToVariable('abcd'));

then I can just use a for and iterate through the keys. I am not sure though this possible at all in JavaScript.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

笨笨の傻瓜 2024-11-15 02:19:12

也许吧:

map.entities.push({abcd: abcd, efgh: efgh, xyz: xyz}[agency]);

对我来说,这种情况需要退后一步,重新思考更多情况,因为它本质上是丑陋的。为什么存在所有这些单独的变量,而不是具有与“代理”值相对应的键的单个对象?

Well maybe:

map.entities.push({abcd: abcd, efgh: efgh, xyz: xyz}[agency]);

To me, this is a situation that calls for stepping back and re-thinking more of the situation, as it's just inherently ugly as it stands. Why are there all these separate variables, instead of a single object with keys corresponding to "agency" values?

寄人书 2024-11-15 02:19:12

只要您知道 agency 值是干净的,就可以在此处使用 eval

map.entities.push(eval(agency));

There's a legitimate case for using eval here as long as you know agency values are clean:

map.entities.push(eval(agency));
忆悲凉 2024-11-15 02:19:12

javascript 中的每个对象都是一个 hashmap(至少是一个 hashmap),这就是这段代码返回 true 的原因:

var obj = {};
obj.test = "someVarVal";
obj.test === obj['test'];

因此,您可以做的是构建一个返回值/函数的对象,如下所示:

var returns = {};
returns['abcd'] = function() {return 'abcd';};
returns['efgh'] = function() {return 'efgh';};

然后执行

map.entities.push(returns[agency]());

如果所有变量都是静态的你可以做得更简单,就像这样:

var returns = {};
returns['abcd'] = 'abcd';
map.entities.push(returns[agency]);

Every object in javascript IS a hashmap (at least sort of a hashmap), that's why this code returns true:

var obj = {};
obj.test = "someVarVal";
obj.test === obj['test'];

So, what you can do is build up an object of returnvalues/functions like so:

var returns = {};
returns['abcd'] = function() {return 'abcd';};
returns['efgh'] = function() {return 'efgh';};

And then do

map.entities.push(returns[agency]());

If all your variables are statics you can do it even simpler, like so:

var returns = {};
returns['abcd'] = 'abcd';
map.entities.push(returns[agency]);
﹂绝世的画 2024-11-15 02:19:12
function _inArr(arr,itm)
{
  for (var i=0; i < arr.length; i++)
  {
     if (arr[i] == itm) return true;
  }
  return false;
}

var allowedAgencies = array['abcd','efgh','xyz','pqrs','values'];
if (_inArr(allowedAgencies, agency))
{
  map.entities.push(window[agency]);
}

_inArr 函数在每个主要的 javascript 包中都可用(例如 $.inArray)。

我认为重点是 JavaScript 是一个很大的哈希图。例如,全局函数名称只是窗口对象的一个​​键。对于任何命名空间项目也是如此。

function _inArr(arr,itm)
{
  for (var i=0; i < arr.length; i++)
  {
     if (arr[i] == itm) return true;
  }
  return false;
}

var allowedAgencies = array['abcd','efgh','xyz','pqrs','values'];
if (_inArr(allowedAgencies, agency))
{
  map.entities.push(window[agency]);
}

The _inArr function is available in every major javascript package ($.inArray, for example).

I think the point is that JavaScript is a big hashmap. A global function name, for instance, is just a key into the window object. The same goes for any namespaced items.

混浊又暗下来 2024-11-15 02:19:12
var agency='foo',
    wanted_words=new Regexp(/^abcd|efgh|xyz|pqrs|values$/);

agency.match(wanted_words) && map.entities.push(eval(agency));

假设代理的所有可能值都作为变量存在。

var agency='foo',
    wanted_words=new Regexp(/^abcd|efgh|xyz|pqrs|values$/);

agency.match(wanted_words) && map.entities.push(eval(agency));

Assumes all possible values of agency exist as variables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文