我怎样根据自定义逻辑将一个数组扩展到与另一个数组的相同长度?

huangapple go评论56阅读模式
英文:

How can I extend an array to fit same length of another array based on custom logic?

问题

我有两个不同的数组:

const texts = ['text1', 'text2', 'text3', 'text4']
const images = ['image1', 'image2']

我遍历文本并显示相应的img,但我始终有更少的图像。

所以我想要将我的图像数组转换为显示两次image1和两次image2

const images = ['image1', 'image1', 'image2', 'image2']

数据来自一个API,所以我可以有一个包含9个元素的文本数组和一个包含4个元素的图像数组。我想要始终有完美的重复:

['image1', 'image1', 'image2', 'image2', 'image3', 'image3', 'image4', 'image4', 'image4']

这是一个最小的示例:

const texts = ['text1', 'text2', 'text3', 'text4', 'text5']
const images = ['image1', 'image2']

return texts.map((text, index) => (
   <div>
     {text}
     <img src={images[index]} // 不起作用
   </div>
))

图像应均匀分布在文本上,如果文本数是奇数,最后一个图像应再次包含在内。

英文:

I have two different arrays:

const texts = [&#39;text1&#39;, &#39;text2&#39;, &#39;text3&#39;, &#39;text4&#39;]
const images = [&#39;image1&#39;, &#39;image2&#39;]

I map over the text and display img corresponding but I always have fewer images.

So I want to transform my images array to display image1 two times and image2 two times:

const images = [&#39;image1&#39;, &#39;image1&#39;, &#39;image2&#39;, &#39;image2&#39;]

Data come from an API so I can have a array of text with 9 elements and array of images with 4 elements. I want to always have a perfect repetition:

[&#39;image1&#39;, &#39;image1&#39;, &#39;image2&#39;, &#39;image2&#39;, &#39;image3&#39;, &#39;image3&#39;, &#39;image4&#39;, &#39;image4&#39;, &#39;image4&#39;]

Here is a minimal example:

const texts = [&#39;text1&#39;, &#39;text2&#39;, &#39;text3&#39;, &#39;text4&#39;, &#39;text5&#39;]
const images = [&#39;image1&#39;, &#39;image2&#39;]

return texts.map((text, index) =&gt; &lt;div&gt;
   {text}
   &lt;img src={images[index]} // NOT work
 &lt;/div&gt;))


// Expected images = [&#39;image1&#39;, &#39;image1&#39;, &#39;image2&#39;, &#39;image2&#39;, &#39;image2&#39;]

Images should be distributed evenly over the texts, and if there's an odd number of texts the last image should be included one more time.

Thanks for your help.

答案1

得分: 1

你可以通过首先计算图像中缺少的项目数量,然后将其除以它们的计数来实现期望的结果。

通过这样做,你可以知道每个项目需要多少个。

然后,你只需遍历数组并重复每个项目的次数(加上已存在的项目)。

最后,我们计算还缺少多少,然后根据需要多次添加最后一个项目。

const texts = ['text1', 'text2', 'text3', 'text4', 'text5', 'text6', 'text7'];
const images = ['image1', 'image2', 'image3'];
const images_new = [];

var remaining = texts.length - images.length;
var count = remaining / images.length;
for (i = 0; i < images.length; i++) {
  for (j = 0; j < Math.floor(count) + 1; j++) {
    images_new.push(images[i]);
  }
}

var remaining = texts.length - images_new.length;
for(i=0; i < remaining; i++){
  images_new.push(images_new.slice(-1)[0]);
}

console.log(images_new);

这段代码实现了你所描述的功能。

英文:

You can achieve the expected result by firstly calculating how many items missing from the images then dividing it by the count of them.

By this you can get how many you need from each.

Then you just walk trough the array and repeat each item by the number you got before (plus the existing one).

Lastly we calculate how many is missing and then add the last item again as many times as needed.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const texts = [&#39;text1&#39;, &#39;text2&#39;, &#39;text3&#39;, &#39;text4&#39;, &#39;text5&#39;, &#39;text6&#39;, &#39;text7&#39;];
const images = [&#39;image1&#39;, &#39;image2&#39;, &#39;image3&#39;];
const images_new = [];

var remaining = texts.length - images.length;
var count = remaining / images.length;
for (i = 0; i &lt; images.length; i++) {
  for (j = 0; j &lt; Math.floor(count) + 1; j++) {
    images_new.push(images[i]);
  }
}

var remaining = texts.length - images_new.length;
for(i=0;i&lt;remaining;i++){
  images_new.push(images_new.slice(-1)[0]);
}

console.log(images_new);

<!-- end snippet -->

答案2

得分: 1

简单的数学运算。取文本索引的数量,除以您拥有的图像数量。这样您就知道有多少个分组。如果有多余的部分,将其设置为最后一个图像索引。

const texts = ['text1', 'text2', 'text3', 'text4', 'text5'];
const images = ['image1', 'image2'];

const num = Math.floor(texts.length / images.length);

const result = texts.map((text, index) => {
  const imageIndex = Math.min(Math.floor(index/num), images.length - 1); 
  return `${text} - ${images[imageIndex]}`;
});

console.log(result)
英文:

Simple math. Take the number of text indexes and divide it by the number of images you have. So you know how many groupings. If you have any extra you set it to the last image index.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const texts = [&#39;text1&#39;, &#39;text2&#39;, &#39;text3&#39;, &#39;text4&#39;, &#39;text5&#39;];
const images = [&#39;image1&#39;, &#39;image2&#39;];

const num = Math.floor(texts.length / images.length);

const result = texts.map((text, index) =&gt; {
  const imageIndex = Math.min(Math.floor(index/num), images.length - 1); 
  return `${text} - ${images[imageIndex]}`;
});

console.log(result)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月16日 00:22:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76483685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定