You need to enable JavaScript to run this app. JavaScript 从嵌套响应对象中筛选母店并提取slug数组的实现方法
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript 从嵌套响应对象中筛选母店并提取slug数组的实现方法

问题描述

我有一份带数组结构的接口返回数据,完整结构如下:

{
  "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"
        }
      ]
    }
  }
}

我的需求是:从上述shops数组的每一项里,筛选出所有满足is_mother_shop: true条件的条目,最终把这些条目对应的slug值提取出来,组装成一个独立的数组。我知道要用JavaScript的数组过滤逻辑实现,但一直没写出正确的代码,不清楚具体怎么写。

实现方法

首先要注意目标数组的实际访问路径:你要处理的店铺数组并不是直接挂在返回对象顶层,而是在data.shops.shops的位置,路径写错是这类问题最常见的踩坑点。
实现逻辑只需要两步链式调用即可:

  1. 用filter方法筛出数组中is_mother_shop为真的条目
  2. 用map方法把筛出来条目的slug字段提取出来,拼成目标数组

核心实现代码如下:

// 假设接口返回的完整对象存在response变量中
const motherShopSlugs = response.data.shops.shops
  // 筛选出满足is_mother_shop为true的店铺项
  .filter(shop => shop.is_mother_shop)
  // 提取每个项的slug字段
  .map(shop => shop.slug);

按照你给出的示例数据,3个店铺的is_mother_shop都为true,最终运行得到的数组结果为:

[
  "catalog-shop-test-30ac6c82",
  "catalog-shop-test-2-4adb1b9",
  "catalog-shop-test-3-1e241f30"
]

提升健壮性写法:如果担心接口返回结构缺失某层导致代码报错,可以搭配可选链操作符处理异常场景,找不到目标数组时直接返回空数组:

const motherShopSlugs = response?.data?.shops?.shops?.length
  ? response.data.shops.shops.filter(shop => shop.is_mother_shop).map(shop => shop.slug)
  : [];

内容的提问来源于stack exchange,提问作者ashfaqrafi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.22 19:45:04