Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
102 views
in Technique[技术] by (71.8m points)

javascript - How do I get the name from object and assign it to a new created element?

My question is:

  1. How do I create a new p with every name value from an object?
  2. Same for img, reviews etc.

I am struggling because I get all the names but I don't know how to create new paragraphs for every index separately (appendData function) and the other function it is just bad and duplicate code (generateImg function)

const data = {
  "arr": [{
    "name": "Box",
    "reviews": 65,
    "img": 1
  }, {
    "name": "Drawer",
    "reviews": 70,
    "img": 2
  }, {
    "name": "Desk",
    "reviews": 20,
    "img": 3
  }, {
    "name": "Chair",
    "reviews": 76,
    "img": 4
  }, {
    "name": "Sofa",
    "reviews": 90,
    "img": 5
  }]
}

function appendData(data) {
  for (var i = 0; i < data.arr.length; i++) {
    var p = document.createElement("p");
    var obj = data.arr[i];
    p.textContent = obj.name;
    var elem = document.getElementById('new');
    elem.appendChild(p);
  }
}


function generateImg(data) {
  for (i = 0; i < data.arr.length; i++) {
    let productImage1 = document.getElementById('productImage1').src = "./img/" + data.arr[0].img + ".jpg";
    let productImage2 = document.getElementById('productImage2').src = "./img/" + data.arr[1].img + ".jpg";
    let productImage3 = document.getElementById('productImage3').src = "./img/" + data.arr[2].img + ".jpg";
    let productImage4 = document.getElementById('productImage4').src = "./img/" + data.arr[3].img + ".jpg";
    let productImage5 = document.getElementById('productImage5').src = "./img/" + data.arr[4].img + ".jpg";
  }
}
<div class="row">
  <div class="media">
    <img id="productImage1" src="" alt="Box">
  </div>
</div>

<div class="row">
  <div class="product-title"></div>
</div>

<div class="row">
  <div class="reviews"></div>
</div>





<div class="row">
  <div class="media">
    <img id="productImage2" src="" alt="Drawer">
  </div>
</div>

<div class="row">
  <div class="product-title"></div>
</div>

<div class="row">
  <div class="reviews"></div>
</div>
question from:https://stackoverflow.com/questions/65939998/how-do-i-get-the-name-from-object-and-assign-it-to-a-new-created-element

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You mean like this? No need for a P when you have a div for each thing

const data = {
  "arr": [
    { "name": "Box",    "reviews": 65, "img": 1 }, 
    { "name": "Drawer", "reviews": 70, "img": 2 }, 
    { "name": "Desk",   "reviews": 20, "img": 3 }, 
    { "name": "Chair",  "reviews": 76, "img": 4 }, 
    { "name": "Sofa",   "reviews": 90, "img": 5 }
  ]
}

const html = data.arr.map(item => `<div class="row">
  <div class="media"><img src=".img/${item.img}.jpg" /></div>
</div>
<div class="row">
  <div class="product-title">${item.name}</div>
</div>
<div class="row">
  <div class="reviews">${item.reviews}</div>
</div>`)

document.getElementById("container").innerHTML = html.join("")
<div id="container"></div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...