如何在ES5中使用计算的属性名称? [英] How to use a computed property name in ES5?

查看:65
本文介绍了如何在ES5中使用计算的属性名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个计算出的属性名称。我看到您可以在ES6中使用它。但是它应该与IOS Webview兼容。所以我不能使用ES6。同样,
的计算名称在循环内也将是相同的,如果这样会使人更容易的话。

I would like to have a computed property name. I saw you can have this in ES6. But it should be compatible with IOS Webview. So I can't use ES6. Also the computed name will be ever the same inside the loop, if this makes it easier for somebody.

有任何想法吗?

var today = moment().format('DD.MM.YY');
for (var i = 0; i < 5; i++) {
    initialData.push(
         {
            dates: {
                "01.01.01": false
                 // instead of 01.01.01 i would like to have the value of today as the key
            }
        }
    )
}


推荐答案

您必须在ES5中精心制作:

You have to do it the elaborate way in ES5:

var today  = moment().format('DD.MM.YY');
var obj    = {};
obj[today] = false;
for (var i = 0; i < 5; i++) {
  initialData.push({ dates: obj });
}

(或移动 obj (如果每次迭代都不相同)

(or move the creation of obj inside the loop if it's different for each iteration)

这篇关于如何在ES5中使用计算的属性名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆