简体   繁体   English

使用foreach追加一个孩子

[英]appending a child using foreach

Supposed I have a response of 假设我有一个回应

在此处输入图片说明

And I loop through the array via 我通过遍历数组

    response.data.aTweets.forEach(element => {
        //console.log(element);
        element.pics.forEach(pic => {
            var image = new Image();
            image.height = 180;
            image.classList.add('mt-2','rounded','mr-2', 'w-100');
            image.src    = pic;
            oHome.oImageRow1.appendChild(image); //append to image row one if length is <= 2
            oHome.oImageRow2.appendChild(image); //append to image row one if length is > 2
        });
        oHome.oTweetContainer.appendChild(oHome.oClone);
    });

and this my oImageRow1 and oImageRow2 这是我的oImageRow1和oImageRow2

<div class="row" id="image_row_one">

</div>

<div class="row" id="image_row_two">

</div>

How can I append to image row one if the length of the element.pics is greater than 2 如果element.pics的长度大于2,如何附加到图像第一行

I tried to use the element.pics.length however it is constant. 我尝试使用element.pics.length,但是它是恒定的。 I think i might need a counter or something 我想我可能需要柜台或其他东西

This should work. 这应该工作。

  response.data.aTweets.forEach(element => { //console.log(element); element.pics.forEach(pic => { var image = new Image(); image.height = 180; image.classList.add('mt-2','rounded','mr-2', 'w-100'); image.src = pic; element.pics.length > 2 ? oHome.oImageRow2.appendChild(image) : oHome.oImageRow1.appendChild(image); }); oHome.oTweetContainer.appendChild(oHome.oClone); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM