JavaScript - 如何打印对象的所有值

标签 javascript

我正在尝试从该数组中获取一个对象并将其所有值打印到 div 中,这可能吗

function Car(company, name, price, details, image, alt){ 
        this.company = company;
        this.name = name;
        this.price = price;
        this.details = details;
        this.image = image;
        this.alt = alt;
    }

var carInformation = [new Car("Company: Ferrari", "Name: Marenello", "Price: $250,000", "Details: Fast. Very Fast.", "images/ferrari.jpg","image of ferrari"),
                          new Car("Company: Dodge", "Name: Viper", "Price: $100,000","Details: Great Acceleration","images/dodge.jpg", "image of viper"),
                          new Car("Company: Ford", "Name: Shelby", "Price: $80,000", "Details: Muscle Car", "images/mustang.jpg", "image of mustang"),
                          new Car("Company: Back To The Future", "Name: Delorean", "Price: $20,000", "Details: Travels through time","images/delorean.jpg", "image of delorean"),
                          new Car("Company: Lamborghini", "Name: Diablo", "Price: $250,000", "Details: Fastest","images/lambo.jpg","image of lamborghini"),
                          new Car("Company: Mercedes Benz", "Name: SLR", "Price: $180,000", "Details: Classy Vehicle.","images/benz.jpg","image of mercedes benz"),
                          new Car("Company: Chevrolet", "Name: Corvette", "Price: $70,000", "Details: Fiberglass body Light Vehicle.","images/vette.jpg","image of corvette"),
                          new Car("Company: Porsche", "Name: Carrera", "Price: $120,000", "Details: Great Handling.","images/porsche.jpg", "image of porsche"),
                          new Car("Company: Audi", "Name: R8", "Price: $110,000", "Details: Fast and Classy.", "images/audi.jpg","image of audi") ];

    for(i=0;i<carInformation.length;i++) {
    $('#container').append('<div class="product"><li><a href="#" ><img src="' + carInformation[i].image + '" alt="' + carInformation[i].alt +'" ></img></a></li><div class="description"><p class="carCompany">'+ carInformation[i].company +'</p><p class="carName">'+ carInformation[i].name +'</p><p class="carPrice">'+ carInformation[i].price +'</p><p class="carDetails">'+ carInformation[i].details +'</p></div></div>');
    $('#productPage').append('<div id="carInfo">' + carInformation[i] + '</div>');

        }

“#container”行有效。是否可以只打印所有值而不写入每个属性?就像我在“#productPage”中尝试的那样? 它返回对象Object。为什么?当我写 console.log(carInformation[0]); 时我能够看到对象中的所有值。

最佳答案

carInformation[i] 是一个对象。你能做的最好的事情就是,向你的汽车添加一个原型(prototype)方法,这样它就会溢出所有属性,然后它就可以在 div 中渲染。

function view(){
return "Company " + this.company + ",Name   " + this.Name + ", Price " + this.price ;
}


Car.prototype.view = view;

//and then use it inside div
 $('#productPage').append('<div id="carInfo">' + carInformation[i].view + '</div>');

您可以通过渲染 HTML 而不是纯字符串来控制汽车信息的显示方式。

关于JavaScript - 如何打印对象的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11099777/

相关文章:

javascript - 如何使用Buffer.from创建Buffer View

javascript - 由于 JS 返回 false,链接不可点击

javascript - 集合教程中的 ko.observable 变量

javascript - Websocket - WSS 在视频录制中无法在 HTTPS 上工作

knockout.js - 是否有一个公认的约定来处理用户输入之前为 "yes-no"的 "unset"下拉菜单?

Javascript:单击主体中除其中一个元素之外的任何位置

javascript - Node handbrakejs 运行代码但不输出

javascript - Google Map API Marker 在 Ruby On Rails 中添加动态标签

javascript - 在 componentDidMount 中 Promise 解析后响应 setState

javascript - 尝试在 <button> 元素上使用类似 href 的东西