在 React Native 中使用过滤器时,图像源正在转换为整数
Images source is converting into integer number while using filter in react native
我有以下 json 格式的数据
const data = [
{
"id": 1,
"category": "fruits",
"title" : "Red Apples",
"image1": require("../assets/images/products/fruits/apple.jpg"),
"image2": require("../assets/images/products/fruits/apple2.jpg"),
"image3": require("../assets/images/products/fruits/apple3.jpg"),
"price": 4.99,
"label": "fresh",
"qty": "200gm"
},
{
"id": 2,
"category": "fruits",
"title" : "Organic Banana",
"image1": require("../assets/images/products/fruits/banana.jpg"),
"image2": require("../assets/images/products/fruits/banana2.jpg"),
"image3": require("../assets/images/products/fruits/banana3.jpg"),
"price": 2.50,
"label": "new",
"qty": "500gm"
},
{
"id": 3,
"category": "fruits",
"title": "Fresh Grapes",
"image1": require("../assets/images/products/fruits/grapes.jpg"),
"image2": require("../assets/images/products/fruits/grapes2.jpg"),
"image3": require("../assets/images/products/fruits/grapes3.jpg"),
"price": 10.69,
"label": "sale",
"qty": "1kg"
},
]
我正在使用过滤器来获取特定 ID 的对象,如下所示
const filterData = null;
filterData = data.filter((item) => item.id == pro_id);
console.log(filterData);
过滤器工作正常,但图像 source/uri 正在转换为数字
[{"category": "fruits", "id": 1, "image1": 15, "image2": 16, "image3": 17, "label": "fresh", "price": 4.99, "qty": "200gm", "title": "Red Apples"}]
而不是图像路径它显示的是数字,如 15、16、17 等
不知道为什么会变
请提前致谢
require
没有 return 图片路径。它不是字符串,因此也不是“json 格式”。请省略 require
并仅将路径存储在“imageX”中以获得所需的输出。
我有以下 json 格式的数据
const data = [
{
"id": 1,
"category": "fruits",
"title" : "Red Apples",
"image1": require("../assets/images/products/fruits/apple.jpg"),
"image2": require("../assets/images/products/fruits/apple2.jpg"),
"image3": require("../assets/images/products/fruits/apple3.jpg"),
"price": 4.99,
"label": "fresh",
"qty": "200gm"
},
{
"id": 2,
"category": "fruits",
"title" : "Organic Banana",
"image1": require("../assets/images/products/fruits/banana.jpg"),
"image2": require("../assets/images/products/fruits/banana2.jpg"),
"image3": require("../assets/images/products/fruits/banana3.jpg"),
"price": 2.50,
"label": "new",
"qty": "500gm"
},
{
"id": 3,
"category": "fruits",
"title": "Fresh Grapes",
"image1": require("../assets/images/products/fruits/grapes.jpg"),
"image2": require("../assets/images/products/fruits/grapes2.jpg"),
"image3": require("../assets/images/products/fruits/grapes3.jpg"),
"price": 10.69,
"label": "sale",
"qty": "1kg"
},
]
我正在使用过滤器来获取特定 ID 的对象,如下所示
const filterData = null;
filterData = data.filter((item) => item.id == pro_id);
console.log(filterData);
过滤器工作正常,但图像 source/uri 正在转换为数字
[{"category": "fruits", "id": 1, "image1": 15, "image2": 16, "image3": 17, "label": "fresh", "price": 4.99, "qty": "200gm", "title": "Red Apples"}]
而不是图像路径它显示的是数字,如 15、16、17 等 不知道为什么会变
请提前致谢
require
没有 return 图片路径。它不是字符串,因此也不是“json 格式”。请省略 require
并仅将路径存储在“imageX”中以获得所需的输出。