如何将带有卡数据的整个阵列传输到函数? JavaScript

const initialCards = [
  {
      name: 'a',
      link: '.jpg'
  },
  {
      name: 'Челябинская область',
      link: '.jpg'
  },
  {
      name: 'b',
      link: '.jpg'
  },
  {
      name: 'c',
      link: '.jpg'
  },
  {
      name: 'd',
      link: '.jpg'
  },
  {
      name: 'e',
      link: '.jpg'
  }
];

这里是函数,目前传递了各个参数。

function createCard(name, link) {
  const cardTemplate = document.querySelector('#card-template').content;
  const cardElement = cardTemplate.cloneNode(true);
  cardElement.querySelector('.place__name').textContent = name;
  cardElement.querySelector('.place__image').src = link;

  cardElement.querySelector('.place__like').addEventListener('click', handleClickLike);
  cardElement.querySelector('.place__image').addEventListener('click', handleDigitClick);


  return cardElement;
}
回答如下:

您可以遍历数组,并为每个项目调用函数

const createdCards = initialCards.map(card => createCard(card.name, cart.link));

如何将带有卡数据的整个阵列传输到函数? JavaScript

const initialCards = [
  {
      name: 'a',
      link: '.jpg'
  },
  {
      name: 'Челябинская область',
      link: '.jpg'
  },
  {
      name: 'b',
      link: '.jpg'
  },
  {
      name: 'c',
      link: '.jpg'
  },
  {
      name: 'd',
      link: '.jpg'
  },
  {
      name: 'e',
      link: '.jpg'
  }
];

这里是函数,目前传递了各个参数。

function createCard(name, link) {
  const cardTemplate = document.querySelector('#card-template').content;
  const cardElement = cardTemplate.cloneNode(true);
  cardElement.querySelector('.place__name').textContent = name;
  cardElement.querySelector('.place__image').src = link;

  cardElement.querySelector('.place__like').addEventListener('click', handleClickLike);
  cardElement.querySelector('.place__image').addEventListener('click', handleDigitClick);


  return cardElement;
}
回答如下:

您可以遍历数组,并为每个项目调用函数

const createdCards = initialCards.map(card => createCard(card.name, cart.link));