【问题标题】:Create an Array from for each and array from in ES6在 ES6 中为每个和数组创建一个数组
【发布时间】:2021-12-17 13:34:05
【问题描述】:

我有一个输入多个字段。我想上传多个文件。 我的问题是我想从中返回一个数组。

<input
  id="file"
  type="file"
  accept="image/*"
  multiple
  onChange={(e) => uploadImage(e.currentTarget.files)}
/>;

const uploadImage = (e) => {
  const files = e;

  Array.from(files).forEach((file) => console.log(file));

  const images = files.map((image) => ({
    imageName: image.name,
    imageFile: image,
  }));
  console.log(images);
};

【问题讨论】:

    标签: javascript arrays reactjs ecmascript-6


    【解决方案1】:

    ...返回数组(在日志中)..:

    const uploadImage = (e) => {
        if(!e) return;
        const files = e;
        const fileList = Array.from(files);
        fileList.forEach((file) => console.log(file));
    
        const images = fileList.map((image) => ({
          imageName: image.name,
          imageFile: image,
        }));
        console.log(images);
      };
    
      return (
        <>
          <input
            id="file"
            type="file"
            accept="image/*"
            multiple
            onChange={(e) => uploadImage(e.currentTarget.files)}
          />;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      相关资源
      最近更新 更多