'A way to access json data
I am importing a json file as 'data' to use it as a initial state of chatList. However,
Property does not exist on type
keeps showing up. Is there a way that I can access chats in data? -> data is an array too, so it should be for example data[0].chats – Apostolos : Thanks a lot!
[
{
"partnerId": "user1",
"chats": [
{
"userId": "user0",
"message": "Olaf, you are melting!",
"msgId": 1644809969390
},
{
"userId": "user1",
"message": "Some people are worth melting for",
"msgId": 1644809969387
}
]
},
{
"partnerId": "user2",
"chats": [
{
"userId": "user2",
"message": "11111",
"msgId": 1644809969392
},
How can I access certain partnerId so that I can filter chats and set as the initial state of chatList?
Solution 1:[1]
If you want to filter by a specific partnerId, then you should do the following:
const [chatList, setChatList] = useState<IChat[]>(data
.filter(dt => dt.partnerId === 'ABCD')
.map(t => t.chats).flat()
.filter(user => user.userId === 'XYZ')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Apostolos |

