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

时间:2012-06-19 11:13:57

标签: 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]);我能够看到对象中的所有值。

6 个答案:

答案 0 :(得分:2)

carInformation [i]是一个对象。您可以做的最好的事情是,为您的汽车添加一个原型类型的方法,这样它会溢出所有属性,然后它可以在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而不是纯字符串来控制汽车信息的显示方式。

答案 1 :(得分:2)

试试这个看魔术:

alert(JSON.stringify(carInformation[0]));

答案 2 :(得分:1)

您需要创建一些函数将变量写入div。您可以将这些添加到Car的原型中。

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;
    }

Car.prototype.product = function() {
    return '<li><a href="#"><img src="'+this.image +'"alt="'+this.alt+'"></img></a></li>';
    };

Car.prototype.description = function() {
    return '<p class="carCompany">'+this.company+'</p><p class="carName">'+this.name +'</p><p class="carPrice">'+ this.price +'</p><p class="carDetails">'+ this.details +'</p>';
    };

然后,您可以调用对象上的函数来填充div。

 for(i=0;i<carInformation.length;i++) {
    $('#container').append('<div class="product">'+carInformation[i].product()+'<div class="description">' + carInformation[i].description() + '</div></div>');
    }

答案 3 :(得分:0)

首先:

var car = new Car( ... );
var $ul = $("<ul></ul>");
for(var key in car) {
  $ul.append("<li>" + car[key] + "</li>");
}
$('#container').append($ul);

答案 4 :(得分:0)

您可以通过迭代对象来获取所有属性: 我想,你也可以在其他情况下打印你的物体,我认为,这是一个很好的解决方案 - 迷你模板:)

Car.prototype.view = function( template, separator ) {
  var arr=[], template = template || "<%name%>: <%value%>",
  separator = separator || "\n";
  foreach( var i in this ) { 
    if( this.hasOwnProperty( i ) ){ 

        arr[arr.length] = template.replace("<%name%>",i).replace("<%value%>" , this[i]);
    }

  } 
  return arr.join(separator);
};


// that will be print list of names each property in car :)
console.log( "<ul>" + yourCar.view( "<li><%name%><li>") +"</ul>") ;

这只是一个简单的例子,您可以以某种方式扩展您的目的。 (某些属性的getter例如。 - image属性,getter可能会立即返回图像标记:))

我没有对此进行测试,可能缺少逗号或其他内容。

享受!

答案 5 :(得分:0)

比上述任何一种解决方案都简单得多的解决方案将仅仅是执行以下操作:

for(const [key, value] of Object.entries(car)) {
     //code, key being the property name, and value being the value of said key
}