英文:
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 = ['text1', 'text2', 'text3', 'text4']
const images = ['image1', 'image2']
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 = ['image1', 'image1', 'image2', 'image2']
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:
['image1', 'image1', 'image2', 'image2', 'image3', 'image3', 'image4', 'image4', 'image4']
Here is a minimal example:
const texts = ['text1', 'text2', 'text3', 'text4', 'text5']
const images = ['image1', 'image2']
return texts.map((text, index) => <div>
{text}
<img src={images[index]} // NOT work
</div>))
// Expected images = ['image1', 'image1', 'image2', 'image2', 'image2']
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 = ['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);
<!-- 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 = ['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)
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论