简体   繁体   English

我们可以在js中同时映射两个数组吗?

[英]Can we map over two arrays at same time in js?

I am working on a React project! 我正在研究一个React项目! I want to know how can we achieve this result? 我想知道如何才能实现这一结果?

  • I want to iterate over some names in an array. 我想迭代一个数组中的一些名称。
  • I want to iterate over some links for images in another array. 我想迭代一些链接,用于另一个数组中的图像。

How can we achieve this? 我们怎样才能做到这一点? Or can we iterate by map on both arrays at the same time? 或者我们可以同时在两个阵列上按地图迭代吗?

What do I want? 我想要什么? I want to render a card with text (head) and an image. 我想渲染一张带有文字(头部)和图像的卡片。 I included links and texts in two separate arrays. 我在两个单独的数组中包含了链接和文本。

For example: const projectText = ['x','y','z']; 例如: const projectText = ['x','y','z']; , const imgUrl = ['x'....]; const imgUrl = ['x'....]; , etc 等等

Here's code: 这是代码:

{/*NOTE: Don't forget to optimise code as DRY before final build */}
                  <div ClassName="container">
            {projectText.map((project, index) => (
                    <div key={index} className="project">
                    <h3>{project}</h3>
                    <img key={index} src={`../img/${project}.png`} alt={project}/>
                    <a role="button" className="btn" href={`https://${project}`} target="_blank" rel="noopener noreferrer">Github</a>
                    </div>
                  </div>
                ))}
                {/* experiment train */}

Assuming, the projectText and imageUrl contain the elements in order and have the same number of elements, you can simply access the url from imageUrl array using the index from projectText 假设projectText和imageUrl按顺序包含元素并具有相同数量的元素,您可以使用imageUrl中的索引从projectText数组中访问url。

   <div ClassName="container">
        {projectText.map((project, index) => (
                <div key={index} className="project">
                <h3>{project}</h3>
                <img key={index} src={`../img/${imageUrl[index]}.png`} alt={project}/>
                <a role="button" className="btn" href={`https://${project}`} target="_blank" rel="noopener noreferrer">Github</a>
                </div>
              </div>
            ))}
            {/* experiment train */}

Assuming the length of the arrays are same, you have following options: 假设数组的长度相同,您有以下选项:

  1. Map on one array and use the index property to access both arrays. 在一个数组上映射并使用index属性访问两个数组。
  2. Create another array having information combined from other arrays. 创建另一个包含来自其他数组的信息的数组。

1. 1。

let names = [];
let images = [];

for (let i = 0; i < names.length; i++) {
    console.log(names[i], images[i]);
}

2. 2。

let names = [];
let images = [];
let combined = [];

for (let i = 0; i < names.length; i++) {
    combined.push({
        name: names[i],
        image: images[i]
    });
}

and now use the combined array. 现在使用combined数组。

A better approach will be to maintain an array of objects populated as below 更好的方法是维护一个填充的对象数组,如下所示

const data = [
{projectText: 'a',imageUrl: 'http://linka.com'},
{projectText: 'b',imageUrl: 'http://linkb.com'}
]

Iterating over this array will get you access to both the fields you require. 迭代此数组将使您可以访问所需的两个字段。 Moreover you can add extra items if you need without hassel. 此外,如果您需要没有哈塞尔,您可以添加额外的项目。

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

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