[英]How to map object of nested arrays in react native
I have a data.json
file:我有一个
data.json
文件:
{
"category1":[
{"name":"XYZ","price":100,"instock":true},
{"name":"ABC","price":934,"instock":false},
{"name":"OTR","price":945,"instock":true},
{"name":"SLG","price":343,"instock":true},
{"name":"KGN","price":342,"instock":true},
{"name":"GDS","price":234,"instock":true},
{"name":"KNL","price":934,"instock":true},
{"name":"GLM","price":320,"instock":true},
{"name":"DKF","price":394,"instock":false},
{"name":"VFS","price":854,"instock":true}
],
"category2":[
{"name":"NA","price":124,"instock":true},
{"name":"DS","price":953,"instock":true},
{"name":"HF","price":100,"instock":true},
{"name":"FJ","price":583,"instock":true},
{"name":"LS","price":945,"instock":false},
{"name":"TR","price":394,"instock":true},
{"name":"PD","price":35,"instock":true},
{"name":"AL","price":125,"instock":true},
{"name":"TK","price":129,"instock":true},
{"name":"PG","price":294,"instock":true}
]
}
I am storing all these data in my reducer of redux and then using hooks to store in my state:我将所有这些数据存储在我的 redux 减速器中,然后使用挂钩存储在我的 state 中:
const [items, setItems] = useState({});
setItems(createMenuReducer.data.menu)
console.log(items);
when console my items
I get the following output:当控制台我的
items
时,我得到以下 output:
{cat1: Array(10), cat2: Array(10), default: {…}}
cat1: (10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
cat2: (10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
default: {cat1: Array(10), cat2: Array(10)}
__proto__: Object
I want to map through this items
and display the name
and price
on the screen.我想通过这个
items
map 并在屏幕上显示name
和price
。
{
createMenuReducer &&
items?.map((item, index) => {
return (
<Text>item</Text>
);
})
}
But I am getting error: items.map is not a function
.但我收到错误:
items.map is not a function
。
I know my way of mapping is wrong, But I always gets confused when mapping something.我知道我的映射方式是错误的,但是在映射某些东西时我总是感到困惑。 Please someone explain me how I can display name and price with their category names ie
category1
and category2
.请有人解释我如何用他们的类别名称显示名称和价格,即
category1
和category2
。
The problem is that .map is a function of the array object and you are currently handling a javascript object with key pair values in your items object. The problem is that .map is a function of the array object and you are currently handling a javascript object with key pair values in your items object.
The easiest way to go about doing what you are trying to do would be to say go 关于做你想做的事情的最简单方法是说
for(let key in items){
items[key].map( item => {
console.log(item.name + " : " + item.price);
})
}
Since you can't use Array.map
on objects
, you'll first have to transform that data.由于您不能在
objects
上使用Array.map
,因此您首先必须转换该数据。 Object.values
could be useful in your case. Object.values
在您的情况下可能很有用。 You could use it to get all of the values
from the keys
in your object
and then concatenate the arrays into a single one using Array.reduce
.您可以使用它从
object
中的keys
中获取所有values
,然后使用Array.reduce
将 arrays 连接成一个。 Here's an example of how it can be done:这是如何完成的示例:
const data = { "category1": [{ "name": "XYZ", "price": 100, "instock": true }, { "name": "ABC", "price": 934, "instock": false }, { "name": "OTR", "price": 945, "instock": true }, { "name": "SLG", "price": 343, "instock": true }, { "name": "KGN", "price": 342, "instock": true }, { "name": "GDS", "price": 234, "instock": true }, { "name": "KNL", "price": 934, "instock": true }, { "name": "GLM", "price": 320, "instock": true }, { "name": "DKF", "price": 394, "instock": false }, { "name": "VFS", "price": 854, "instock": true } ], "category2": [{ "name": "NA", "price": 124, "instock": true }, { "name": "DS", "price": 953, "instock": true }, { "name": "HF", "price": 100, "instock": true }, { "name": "FJ", "price": 583, "instock": true }, { "name": "LS", "price": 945, "instock": false }, { "name": "TR", "price": 394, "instock": true }, { "name": "PD", "price": 35, "instock": true }, { "name": "AL", "price": 125, "instock": true }, { "name": "TK", "price": 129, "instock": true }, { "name": "PG", "price": 294, "instock": true } ] }; const final = Object.values(data).reduce((arr, nextArr) => [...arr, ...nextArr], []); console.log(final);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.