skip to Main Content

I have a below data structure

[
  {
    "slug": "vertical-lift-module-market",
    "id": 68055,
    "short_title": "Vertical Lift Module (VLM) Market",
    "related_reports_updated": {
      "sub_categories": [
        {
          "slug": "audience-analytics-market",
          "id": 66684,
          "short_title": "Audience Analytics Market"
        },
        {
          "slug": "mobile-wallet-market",
          "id": 68830,
          "short_title": "Mobile Wallet Market"
        },
        {
          "id": 46625,
          "slug": "north-america-3d-4d-technology-market",
          "short_title": "NA 3D 4D Technology Market"
        },
        {
          "short_title": "North America Chatbot Market",
          "slug": "north-america-chatbot-market",
          "id": 72309
        },
        {
          "short_title": "Optical Wavelength Services Market",
          "slug": "optical-wavelength-services-market",
          "id": 71348
        }
      ],
      "categories": [
        {
          "id": 48402,
          "slug": "artificial-intelligence-impact-and-future-in-modern-warfare",
          "short_title": "AI in Modern Warfare Market"
        },
        {
          "short_title": "Certificate Authority Market",
          "slug": "certificate-authority-market",
          "id": 70769
        },
        {
          "short_title": "Global Mobile Identification Market",
          "slug": "global-mobile-identification-market",
          "id": 91316
        },
        {
          "slug": "identity-verification-market",
          "id": 69133,
          "short_title": "Identity Verification Market"
        },
        {
          "slug": "signature-verification-market",
          "id": 59014,
          "short_title": "Signature Verification Market"
        }
      ],
      "other_reports": [
        {
          "id": 48102,
          "slug": "global-artificial-lift-systems-market-industry",
          "short_title": "Artificial Lift System Market"
        },
        {
          "id": 51724,
          "slug": "latin-america-artificial-lift-systems-market-industry",
          "short_title": "Latin America Artificial Lift System Market"
        },
        {
          "id": 55702,
          "slug": "medical-lifting-sling-market",
          "short_title": "Medical Lifting Slings Market"
        },
        {
          "id": 52020,
          "slug": "north-america-artificial-lift-systems-market-industry",
          "short_title": "North America Artificial Lift Systems Market"
        },
        {
          "slug": "vertical-farming-market",
          "id": 61898,
          "short_title": "Vertical Farming Market"
        }
      ]
    }
  },
  {
    "slug": "united-states-real-estate-services---growth-trends-and-forecast-2022-- -2027",
    "id": 68056,
    "short_title": "United States Real Estate Services Market",
    "related_reports_updated": {
      "sub_categories": [
        {
          "slug": "canada-real-estate-services-market---growth-trends-and-forecast-2020---2025",
          "id": 68051,
          "short_title": "Canada Real Estate Services Market"
        },
        {
          "slug": "germany-real-estate-services-market--growth-trends-and-forecast-2020---2025",
          "id": 68054,
          "short_title": "Germany Real Estate Services Market"
        },
        {
          "short_title": "Office Real Estate Market",
          "slug": "office-real-estate-market",
          "id": 80022
        },
        {
          "slug": "uk-real-estate-services-market---growth-trends-and-forecast-2020---2025",
          "id": 68057,
          "short_title": "United Kingdom Real Estate Services Market"
        },
        {
          "short_title": "United States Senior Living Market ",
          "slug": "united-states-senior-living-market",
          "id": 72583
        }
      ],
      "categories": [
        {
          "slug": "uae-real-estate-market-services",
          "id": 68040,
          "short_title": "United Arab Emirates Real Estate Services Market"
        },
        {
          "id": 46257,
          "slug": "residential-real-estate-market-in-uae",
          "short_title": "United Arab Emirates Residential Real Estate Market"
        },
        {
          "id": 54710,
          "slug": "commercial-real-estate-market-in-usa",
          "short_title": "United States Commercial Real Estate Market"
        },
        {
          "short_title": "United States Luxury Residential Real Estate Market",
          "slug": "united-states-luxury-residential-real-estate-market",
          "id": 90838
        },
        {
          "short_title": "United States Office Real Estate Market",
          "slug": "united-states-office-real-estate-market",
          "id": 72479
        }
      ],
      "other_reports": [
        {
          "short_title": "United States (US) MEP Services Market",
          "slug": "united-states-mep-services-market",
          "id": 71420
        },
        {
          "slug": "united-states-hvac-services-market",
          "id": 67903,
          "short_title": "US HVAC Services Market"
        },
        {
          "short_title": "United States IT Services Market ",
          "slug": "united-states-it-services-market",
          "id": 91565
        },
        {
          "short_title": "United States Managed Services Market",
          "slug": "united-states-managed-services-market",
          "id": 71366
        },
        {
          "short_title": "United States Pet Care and Services Market",
          "slug": "united-states-pet-care-and-services-market",
          "id": 90805
        }
      ]
    }
  },
]

In above data there is related_reports_updated nested object in every object in Array. This nested object contains 3 arrays named categories, sub_categories and other_reports. Is there any way I can combine these arrays to one array?

2

Answers


  1. Let’s break it down without using language-specific features and then use it later.

    dataArr = { ...yourArrayOfTwoObjectsSpecifiedAbove }
    data = dataArr[0];
    
    data.related_reports_updated.categories = [
        ...data.related_reports_updated.categories, 
        ...data.related_reports_updated.sub_categories, 
        ...data.related_reports_updated.other_reports,
    ]
    
    delete data.related_reports_updated.other_reports
    delete data.related_reports_updated.sub_categories
    

    And this is what we want to achieve, let’s make it a bit cleaner, and generalize it, for Array Ai inside related_reports_updated where i is the index, we want to get a result V such that V is the concatenation of all arrays starting from A0 to A(i-1)

    const v = Object.entries(data.related_reports_updated)
              .reduce((acc, [k, ai]) => [...acc, ...ai], [])
    

    And that’s what you need

    Login or Signup to reply.
  2. const data = [{"slug":"vertical-lift-module-market","id":68055,"short_title":"Vertical Lift Module (VLM) Market","related_reports_updated":{"sub_categories":[{"slug":"audience-analytics-market","id":66684,"short_title":"Audience Analytics Market"},{"slug":"mobile-wallet-market","id":68830,"short_title":"Mobile Wallet Market"},{"id":46625,"slug":"north-america-3d-4d-technology-market","short_title":"NA 3D 4D Technology Market"},{"short_title":"North America Chatbot Market","slug":"north-america-chatbot-market","id":72309},{"short_title":"Optical Wavelength Services Market","slug":"optical-wavelength-services-market","id":71348}],"categories":[{"id":48402,"slug":"artificial-intelligence-impact-and-future-in-modern-warfare","short_title":"AI in Modern Warfare Market"},{"short_title":"Certificate Authority Market","slug":"certificate-authority-market","id":70769},{"short_title":"Global Mobile Identification Market","slug":"global-mobile-identification-market","id":91316},{"slug":"identity-verification-market","id":69133,"short_title":"Identity Verification Market"},{"slug":"signature-verification-market","id":59014,"short_title":"Signature Verification Market"}],"other_reports":[{"id":48102,"slug":"global-artificial-lift-systems-market-industry","short_title":"Artificial Lift System Market"},{"id":51724,"slug":"latin-america-artificial-lift-systems-market-industry","short_title":"Latin America Artificial Lift System Market"},{"id":55702,"slug":"medical-lifting-sling-market","short_title":"Medical Lifting Slings Market"},{"id":52020,"slug":"north-america-artificial-lift-systems-market-industry","short_title":"North America Artificial Lift Systems Market"},{"slug":"vertical-farming-market","id":61898,"short_title":"Vertical Farming Market"}]}},{"slug":"united-states-real-estate-services---growth-trends-and-forecast-2022-- -2027","id":68056,"short_title":"United States Real Estate Services Market","related_reports_updated":{"sub_categories":[{"slug":"canada-real-estate-services-market---growth-trends-and-forecast-2020---2025","id":68051,"short_title":"Canada Real Estate Services Market"},{"slug":"germany-real-estate-services-market--growth-trends-and-forecast-2020---2025","id":68054,"short_title":"Germany Real Estate Services Market"},{"short_title":"Office Real Estate Market","slug":"office-real-estate-market","id":80022},{"slug":"uk-real-estate-services-market---growth-trends-and-forecast-2020---2025","id":68057,"short_title":"United Kingdom Real Estate Services Market"},{"short_title":"United States Senior Living Market ","slug":"united-states-senior-living-market","id":72583}],"categories":[{"slug":"uae-real-estate-market-services","id":68040,"short_title":"United Arab Emirates Real Estate Services Market"},{"id":46257,"slug":"residential-real-estate-market-in-uae","short_title":"United Arab Emirates Residential Real Estate Market"},{"id":54710,"slug":"commercial-real-estate-market-in-usa","short_title":"United States Commercial Real Estate Market"},{"short_title":"United States Luxury Residential Real Estate Market","slug":"united-states-luxury-residential-real-estate-market","id":90838},{"short_title":"United States Office Real Estate Market","slug":"united-states-office-real-estate-market","id":72479}],"other_reports":[{"short_title":"United States (US) MEP Services Market","slug":"united-states-mep-services-market","id":71420},{"slug":"united-states-hvac-services-market","id":67903,"short_title":"US HVAC Services Market"},{"short_title":"United States IT Services Market ","slug":"united-states-it-services-market","id":91565},{"short_title":"United States Managed Services Market","slug":"united-states-managed-services-market","id":71366},{"short_title":"United States Pet Care and Services Market","slug":"united-states-pet-care-and-services-market","id":90805}]}}]
    
    const result = data.map(({related_reports_updated, ...rest})=>({
        ...rest,
        related_reports_updated: Object.values(related_reports_updated).flat()
    }))
    
    console.log(result)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search