简体   繁体   English

forEach 循环中的条件语句

[英]Conditional statements within a forEach loop

I am trying to run a unique function to a specific object in an array.我正在尝试将唯一的 function 运行到数组中的特定 object 。

I currently have a forEach loop that iterates through different records in an imported database, which creates a 'panel' for every record.我目前有一个 forEach 循环,它遍历导入数据库中的不同记录,为每条记录创建一个“面板”。

Here is a preview:这是一个预览:

滑块预览

As you can see, the first panel does not line up with the bottom of the others.如您所见,第一个面板与其他面板的底部不对齐。 This is because it is a landscape photo and so its width must me larger to compensate.这是因为它是一张风景照片,所以它的宽度必须更大才能补偿。 However, to do this I must have some kind of exception in my forEach loop to set the width of that record uniquely.但是,要做到这一点,我的 forEach 循环中必须有某种异常来唯一地设置该记录的宽度。

Here is an the code I have written:这是我编写的代码:

fetch(
"https://(apiurl)/" ) //here I run a GET request to my database
.then(handleError) // skips to .catch if error is thrown
.then((data) => {
  data.records.forEach((record) => {
    let galleryitem = document.createElement("li");
    galleryitem.setAttribute("class", "splide__slide is-visible");
    galleryitem.setAttribute("aria-hidden", "false");
    galleryitem.setAttribute("tabindex", "0");
    galleryitem.setAttribute("style", "margin-right: 28px; width: ${index === 0 ? 30 : 25}vw");
    listing.appendChild(galleryitem);
    let gallerycontent = document.createElement("div");
    gallerycontent.setAttribute("class", "slider-square");
    galleryitem.appendChild(gallerycontent);
    let imgwrapper = document.createElement("div");
    imgwrapper.setAttribute("class", "slider-square_img");
    gallerycontent.appendChild(imgwrapper);
    let de_img = document.createElement("img");
    de_img.setAttribute("class", "slider-square_photo");
    de_img.setAttribute("src", record.fields.ArtLink);
    de_img.setAttribute("loading", "lazy");
    de_img.setAttribute("sizes", "(max-width: 479px) 84vw, (max-width: 991px) 48vw, 36vw");
    imgwrapper.appendChild(de_img);
    let textcontent = document.createElement("div");
    textcontent.setAttribute("class", "text-opacity");
    gallerycontent.appendChild(textcontent);
    let art_title = document.createElement("h3");
    art_title.setAttribute("class", "slider_title");
    art_title.textContent = record.fields.Title;
    textcontent.appendChild(art_title);
    let art_desc = document.createElement("h3");
    art_desc.setAttribute("class", "slider_descriptor");
    art_desc.textContent = record.fields.Descriptor;
    textcontent.appendChild(art_desc);
  });
})
.catch(function writeError(err) {
  // catches the error and logs it
})

I have thought of running a conditional statement within the loop so that only the first image has its own width but to no avail.我曾想过在循环中运行条件语句,以便只有第一张图像有自己的宽度,但无济于事。 For example, I would do:例如,我会这样做:

if (data.records.record == [0]) { galleryitem.setAttribute("style", "margin-right: 28px; width: 50vw"); }

This does not work as intended.这不能按预期工作。 Any ideas?有任何想法吗?

(It may also be useful to note is that I am using the splidejs library for the slider) (可能还需要注意的是,我正在使用 splidejs 库作为滑块)

EDIT: I have added the full code for context, and here is a screenshot of a section of the array table >>编辑:我已经添加了上下文的完整代码,这里是数组表部分的屏幕截图 >> 空气表截图

Just check the index of the item you're iterating over?只需检查您正在迭代的项目的索引吗?

data.records.forEach((record, index) => {
    const galleryitem = document.createElement("li");
    galleryitem.setAttribute("aria-hidden", "false");
    galleryitem.setAttribute("tabindex", "0");
    galleryitem.style.cssText = `margin-right: 28px; width: ${index === 0 ? 50 : 25}vw`;
});

You also probably want to append the galleryitem to a container inside the loo;.您可能还想将Galleryitem galleryitem厕所内的容器中;。

You can also do that:你也可以这样做:

 //--
.then( data  =>
  {
  let attributs = { 'aria-hidden':false, tabindex:0, style:'margin-right: 28px; width: 50vw' }  
  data.records.forEach( record =>
    {
    let galleryitem = Object.assign( document.createElement('li'), attributs)
    attributs.style = 'margin-right: 28px; width: 25vw'
    
    // ....
  });
})
.catch(....

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

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