我有一个很大的json格式的客户端数据列表,需要根据条件显示客户端详细信息,例如status='active'
,id=1003
等。
如何在本机反应
示例客户列表
"data": {
"items": [{
"id": 1001,
"name": "CName1",
"address": "New Street 1",
"phone": null,
"status": "active"
},
{
"id": 1002,
"name": "CName2",
"address": "New Street 2",
"phone": null,
"status": "deleted"
},
{
"id": 1003,
"name": "CName3",
"address": "New Street 3",
"phone": null,
"status": "active"
},
{
"id": 1004,
"name": "CName4",
"address": "New Street 4",
"phone": null,
"status": "suspended"
}
]
}
如何从此列表中列出所有“活动”客户端,或在react native中列出客户端ID 1003的详细信息
答案 0 :(得分:0)
这与本机反应无关。如果要从数组中过滤项目,则可以使用.filter()
。
例如,要基于status
进行过滤,您可以执行以下操作:
data.items.filter(item => item.status === 'active')
如果您要查找特定项目(例如,基于id
,因此只过滤一个结果),则可以使用.find()
data.items.find(item => item.id === 1001)
实时示例:
const data = {
"items": [{
"id": 1001,
"name": "CName1",
"address": "New Street 1",
"phone": null,
"status": "active"
},
{
"id": 1002,
"name": "CName2",
"address": "New Street 2",
"phone": null,
"status": "deleted"
},
{
"id": 1003,
"name": "CName3",
"address": "New Street 3",
"phone": null,
"status": "active"
},
{
"id": 1004,
"name": "CName4",
"address": "New Street 4",
"phone": null,
"status": "suspended"
}
]
}
const filtered = data.items.filter(item => item.status === 'active')
console.log(filtered);
答案 1 :(得分:0)
您可以在此处使用过滤器方法。
const statusActiveArr = data.items.filter(item => item.status === 'active');
const specificIdClient = data.items.filter(item => item.id === 1003)[0];