在Javascript中显示来自对象的数据
我有一个包含数据数组的对象在Javascript中显示来自对象的数据,javascript,arrays,reactjs,object,Javascript,Arrays,Reactjs,Object,我有一个包含数据数组的对象 { "success": true, "message": "Merchant and Shop info for auth user", "data": { "merchant": { "status": "Onboarded", "merchant_type":
{
"success": true,
"message": "Merchant and Shop info for auth user",
"data": {
"merchant": {
"status": "Onboarded",
"merchant_type": "Medium",
"merchant_email": "",
"merchant_logo": "",
"merchant_name": "Test Merchant",
"merchant_address": "Dhanmondi 32 Road Bridge, Dhanmondi Bridge, Dhaka 1209, Bangladesh",
"owner": "ishak",
"merchant_phone_no": "01521200079",
"merchant_trade_license_no": "ddww23sa",
"merchant_trade_license_expiration_date": "2004-06-11T00:00:00.000Z",
"owner_nid_no": 981567831,
"owner_nid_image_front": "front-image-url",
"owner_nid_image_back": "back-image-url",
"owner_phone_no": "01521200079",
"owner_email": "owner@email.com",
"merchant_code": "EM38f2f433",
"createdAt": "2021-05-02T06:03:00.417Z"
},
"shops": {
"count": 3,
"shops": [
{
"status": "Unapproved",
"max_allowed_total": 1000000,
"min_allowed_total": 500,
"max_order_quantity": 0,
"max_order_amount": 0,
"min_order_quantity": 0,
"min_order_amount": 0,
"is_delivery_hero_allowed": false,
"is_cod_allowed": false,
"is_mother_shop": true,
"approved": false,
"allowed_payment_methods": [
"bkash",
"sslcommerz_gateway",
"nagad"
],
"shop_type": "Regular",
"name": "catalog shop test",
"address": "merchant address",
"longitude": 1.55,
"latitude": 1.06,
"description": "Long description",
"logo_image": "logo-image-url",
"image": "banner-image-url",
"contact_number": "0123456789",
"merchant_id": "608e4094d33ca200752ca127",
"slug": "catalog-shop-test-30ac6c82",
"createdAt": "2021-05-02T06:18:01.950Z"
},
{
"status": "Unapproved",
"max_allowed_total": 1000000,
"min_allowed_total": 500,
"max_order_quantity": 0,
"max_order_amount": 0,
"min_order_quantity": 0,
"min_order_amount": 0,
"is_delivery_hero_allowed": false,
"is_cod_allowed": false,
"is_mother_shop": true,
"approved": false,
"allowed_payment_methods": [
"bkash",
"sslcommerz_gateway",
"nagad"
],
"shop_type": "Regular",
"name": "catalog shop test 2",
"address": "merchant address",
"longitude": 1.55,
"latitude": 1.06,
"description": "Long description",
"logo_image": "logo-image-url",
"image": "banner-image-url",
"contact_number": "0123456789",
"merchant_id": "608e4094d33ca200752ca127",
"slug": "catalog-shop-test-2-4adb1b9",
"createdAt": "2021-05-02T06:27:05.286Z"
},
{
"status": "Unapproved",
"max_allowed_total": 1000000,
"min_allowed_total": 500,
"max_order_quantity": 0,
"max_order_amount": 0,
"min_order_quantity": 0,
"min_order_amount": 0,
"is_delivery_hero_allowed": false,
"is_cod_allowed": false,
"is_mother_shop": true,
"approved": false,
"allowed_payment_methods": [
"bkash",
"sslcommerz_gateway",
"nagad"
],
"shop_type": "Regular",
"name": "catalog shop test 3",
"address": "merchant address test update",
"longitude": 1.55,
"latitude": 1.06,
"description": "description",
"logo_image": "logo-image-url",
"image": "banner-image-url",
"contact_number": "0123456789",
"merchant_id": "608e4094d33ca200752ca127",
"slug": "catalog-shop-test-3-1e241f30",
"createdAt": "2021-05-02T06:33:59.990Z"
}
]
}
}
}
现在我想列出每个商店数组中的所有slug
,其中是“母亲商店”:是的,
我想我需要在JS中执行一些数组过滤操作,但我无法找到解决方案。我需要创建一个包含所有slug
的数组,其中“is_mother_shop”:true,
用于此响应数据中所有登记的商店
。我不确定什么应该是它的良好实现。您可以使用
slugs = response.data?.shops?.shops
?.filter((p) => p.is_mother_shop)
.map((p) => p.slug);
工作示例:您可以分享您的尝试吗?过滤数组并获取过滤项的slug
属性。您实际上不需要使用map
进行可选链接。如果filter
存在map
也会存在。编辑答案
data.data.shops.shops.filter(el => el.is_mother_shop).map(el => el.slug)