javascript - 如何访问对象数组中的两个单独的图像并将其分配给同一数组中的第三个对象?
问题描述
我正在使用模拟数据来创建卡片组件,有些卡片有单个图像,而其他卡片有并排比较图像。我创建了一个上传数组,前两个对象有一个image
,而对于第三个对象,我需要访问第一个和第二个对象中的图像以形成我的comparisons
对象。我怎样才能做到这一点?我添加了一些有用的评论。
import faker from 'faker';
import uuid from 'uuid/v1';
const fakeUploads = [
{
avatar: faker.image.avatar(),
description: faker.lorem.sentences(),
id: uuid(),
image: faker.image.avatar(),
},
{
avatar: faker.image.avatar(),
description: faker.lorem.sentences(),
id: uuid(),
image: faker.image.avatar(),
name: faker.name.findName(),
},
{
avatar: faker.image.avatar(),
comparisons: { // Object.keys(comparison) => [0, 1]
//Access to the two images in the above object where `0` is the index of the first image and `1` is the indes of the second
},
description: faker.lorem.sentences(),
id: uuid(),
name: faker.name.findName(),
},
];
我已经绘制了模拟上传的值,所以在我的组件中我想要类似的东西
values.includes(comparisons)?
<span>
<img width="50%" alt={UPLOAD} src={comparisons.image[0]} />
<img width="50%" alt={UPLOAD} src={comparisons.image[1]} />
</span> : <img alt={UPLOAD} src={image} />
解决方案
您可以通过将第三个对象添加到范围内的数组来实现。
import faker from 'faker';
import uuid from 'uuid/v1';
const fakeUploads = [
{
avatar: faker.image.avatar(),
description: faker.lorem.sentences(),
id: uuid(),
image: faker.image.avatar(),
},
{
avatar: faker.image.avatar(),
description: faker.lorem.sentences(),
id: uuid(),
image: faker.image.avatar(),
name: faker.name.findName(),
}
]
fakeUploads.push({
avatar: faker.image.avatar(),
comparisons: {[fakeUploads[0].image, fakeUploads[1].image]},
description: faker.lorem.sentences(),
id: uuid(),
name: faker.name.findName(),
})
那是你要找的吗?
推荐阅读
- c# - asp.net core 5中UseWhen和MapWhen的区别
- powershell - Powershell:将 StackOverflow 样式的编码 URL 转换为 HTML
- python - 使用 vultr 连接到已经存在的 postgres 数据库
- javascript - (discord.js)TypeError:尝试发送直接消息时无法读取未定义的属性“发送”
- c++ - 循环决策。寻找更好的方法和理解
- javascript - 将 JavaScript 代码添加到具有相同类的元素的问题
- swift - 带有掩码覆盖块滚动的 ScrollView
- javascript - 赛普拉斯从回调函数中获取值
- javascript - 当我将 div 高度设置为 100vh 时,我的背景图像不断消失
- html - 为没有固定大小的表格创建一个可滚动容器,该容器还可以防止父元素(包括浏览器窗口)上的滚动条