循环遍历对象时,如何以编程方式创建同名变量?

时间:2018-02-15 14:46:13

标签: javascript

我有一些看起来像这样的物品,但列出了几辆车

let snapshot = [
    {
        "vehicle": "2015 Ford Mustang EcoBoost Premium",
        "link": "details9.html",
        "img": "css/images/vehicles/progressiveJPG/spotlight-mustang2.jpg",
        "trim": "GT Premium 2dr Coupe",
        "price": 16998,
        "priceSpecial": 19000,
        "priceAvg": 21470,
        "priceMonthly": 253,
        "miles": 37132,
        "milesAvg": 31029
    }
]

我想用这样的东西进行循环

for (var i = 0; i < snapshot.length; i++) {
    let vehicle = snapshot[i].vehicle;
    let link = snapshot[i].link;
    let img = snapshot[i].img;
    let trim = snapshot[i].trim;
    let price = snapshot[i].price;
    let priceSpecial = snapshot[i].priceSpecial;
    let priceAvg = snapshot[i].priceSpecial;
    let priceMonthly = snapshot[i].priceMonthly;
    let miles = snapshot[i].miles;
    let milesAvg = snapshot[i].milesAvg;
}

然后我会使用模板字符串配置输出并在需要时插入这些变量。我的问题是,我是否可以动态创建与对象键同名的变量,甚至是必要的变量。我应该仅将每个项目称为snapshot[i].vehicle吗?

3 个答案:

答案 0 :(得分:3)

  

我的问题是,我是否可以动态创建与对象键同名的变量,甚至是必要的变量。我应该将每个项目称为快照[i] .vehicle吗?

你目前正在做得很好

for (var i = 0; i < snapshot.length; i++) {
    let vehicle = snapshot[i].vehicle;

    console.log(vehicle)
    console.log(snapshot[i].vehicle)
}

上面的代码打印出两次相同的值,只是通过不同的句柄

天气你应该将所有属性放在相应的变量中,或者你应该直接引用快照[i]基本上取决于你。当代码变得太乱以至于无法轻松阅读时,我通常会选择变量解决方案。

我建议你编写可读性代码而不是一些效率概念,否则你的脚本运行速度会明显加快

答案 1 :(得分:2)

&#13;
&#13;
const snapshot = [{
        "vehicle": "2015 Ford Mustang EcoBoost Premium",
        "link": "details9.html",
        "img": "css/images/vehicles/progressiveJPG/spotlight-mustang2.jpg",
        "trim": "GT Premium 2dr Coupe",
        "price": 16998,
        "priceSpecial": 19000,
        "priceAvg": 21470,
        "priceMonthly": 253,
        "miles": 37132,
        "milesAvg": 31029
}];

    for (var i = 0; i < snapshot.length; i++) {
        const {
           vehicle,
           link, 
           img, 
           trim, 
           price, 
           priceSpecial, 
           priceMonthly, 
           miles, 
           milesAvg
        } = snapshot[i];
     
        console.log(vehicle,
           link, 
           img, 
           trim, 
           price, 
           priceSpecial, 
           priceMonthly, 
           miles, 
           milesAvg)
    }
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您必须创建对象数组,然后您可以检索值作为示例。创建对象数组,如。

let snapshot = [ { "vehicle": "2015 Ford Mustang EcoBoost Premium", "link": "details9.html", "img": "css/images/vehicles/progressiveJPG/spotlight-mustang2.jpg", "trim": "GT Premium 2dr Coupe", "price": 16998, "priceSpecial": 19000, "priceAvg": 21470, "priceMonthly": 253, "miles": 37132, "milesAvg": 31029 },{ "vehicle": "2015 Ford Mustang EcoBoost Premium", "link": "details9.html", "img": "css/images/vehicles/progressiveJPG/spotlight-mustang2.jpg", "trim": "GT Premium 2dr Coupe", "price": 16998, "priceSpecial": 19000, "priceAvg": 21470, "priceMonthly": 253, "miles": 37132, "milesAvg": 31029 },{ "vehicle": "2015 Ford Mustang EcoBoost Premium", "link": "details9.html", "img": "css/images/vehicles/progressiveJPG/spotlight-mustang2.jpg", "trim": "GT Premium 2dr Coupe", "price": 16998, "priceSpecial": 19000, "priceAvg": 21470, "priceMonthly": 253, "miles": 37132, "milesAvg": 31029 }]