0

我正在尝试从该数组中获取一个对象并将其所有值打印到一个 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”中尝试的那样?它返回对象对象。为什么?当我写 console.log(carInformation[0]); 我能够看到对象中的所有值。

4

6 回答 6

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 而不是纯字符串来控制汽车信息的显示方式。

于 2012-06-19T11:17:26.027 回答
2

试试这个,看看神奇:

alert(JSON.stringify(carInformation[0]));
于 2012-06-19T11:22:38.740 回答
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>');
    }
于 2012-06-19T11:39:56.540 回答
0

对于初学者:

var car = new Car( ... );
var $ul = $("<ul></ul>");
for(var key in car) {
  $ul.append("<li>" + car[key] + "</li>");
}
$('#container').append($ul);
于 2012-06-19T11:24:02.747 回答
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 可能会立即返回图像标签 :))

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

享受!

于 2012-06-19T11:33:19.507 回答
0

比任何这些都简单得多的解决方案只是做这样的事情:

for(const [key, value] of Object.entries(car)) {
     //code, key being the property name, and value being the value of said key
}
于 2020-11-20T19:17:44.293 回答