Locate elements within an array based on certain internal characteristics

I have an array of objects that contain a property called "distributionChannel". I need to find objects within the array that have a value of "sellChannel" for the key in the "distributionChannel" property.

Here is my array:

[
  {
    "lineItems": [
      {
        "id": "92477254-088f-4b6d-bb87-ba670b427eec",
        "distributionChannel": {
          "id": "9c5b1a2a-52dd-4cef-b914-94d2332c5c4f",
          "key": "buyChannel"
        },
        "__typename": "LineItem"
      }
    ],
    "id": "ef6ab2b4-538d-4490-a491-1db561101590",
    "__typename": "Order"
  },
  {
    "lineItems": [
      {
        "id": "93041518-7766-4625-a596-567d41606db9",
        "distributionChannel": {
          "id": "9c5b1a2a-52dd-4cef-b914-94d2332c5c4f",
          "key": "sellChannel"
        },
        "__typename": "LineItem"
      }
    ],
    "id": "06814bde-89a8-4009-a8af-87e031dd10ef",
    "__typename": "Order"
  },
  {
    "lineItems": [
      {
        "id": "81b9aa6c-c631-429b-8227-f21100968720",
        "distributionChannel": {
          "id": "002f67cb-7f2e-48d2-a164-01b2857fcc59",
          "key": "sellChannel"
        },
        "__typename": "LineItem"
      },
      {
        "id": "003797a0-3142-4162-849b-3f0661e3aff5",
        "distributionChannel": {
          "id": "002f67cb-7f2e-48d2-a164-01b2857fcc59",
          "key": "sellChannel"
        },
        "__typename": "LineItem"
      }
    ],
    "id": "631b9945-c30e-4c92-9a01-7635988c7c78",
    "__typename": "Order"
  }
]

I need to search for objects that have

distributionChannel.key === 'sellChannel'
within the lineItems property.

Expected output:

[
  {
    "lineItems": [
      {
        "id": "93041518-7766-4625-a596-567d41606db9",
        "distributionChannel": {
          "id": "9c5b1a2a-52dd-4cef-b914-94d2332c5c4f",
          "key": "sellChannel"
        },
        "__typename": "LineItem"
      }
    ],
    "id": "06814bde-89a8-4009-a8af-87e031dd10ef",
    "__typename": "Order"
  },
  {
    "lineItems": [
      {
        "id": "81b9aa6c-c631-429b-8227-f21100968720",
        "distributionChannel": {
          "id": "002f67cb-7f2e-48d2-a164-01b2857fcc59",
          "key": "sellChannel"
        },
        "__typename": "LineItem"
      },
      {
        "id": "003797a0-3142-4162-849b-3f0661e3aff5",
        "distributionChannel": {
          "id": "002f67cb-7f2e-48d2-a164-01b2857fcc59",
          "key": "sellChannel"
        },
        "__typename": "LineItem"
      }
    ],
    "id": "631b9945-c30e-4c92-9a01-7635988c7c78",
    "__typename": "Order"
  }
]

Any suggestions on how to achieve this?

Answer №1

Filter the array to only include elements that have list items where some contain a distribution key equal to sellChannel:

const filteredResults = originalArray.filter(item => 
  item.listItems.some(subItem => 
    subItem.distribution.key === 'sellChannel'))

It's important to note that if an item contains both sellChannel and buyChannel entries, it will still be included in the results.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Avoid the issue of markers clustering in Leaflet to have distinct markerClusterGroup icons without overlap

Is there a way to prevent two separate markerClusterGroups from overlapping in Leaflet? I have one with a custom Icon to differentiate between the two clusters, but for simplicity's sake, I've omitted that part in this example. var map = L.map(&q ...

Utilizing multiple components onEnter with React-router for authentication scenarios

I am currently working on an app that consists of 2 components for a single path. The routes are as follows: <Router history={hashHistory}> <Route path="/" component={AppContainer} onEnter={requireEnter}> <Route path="/homepage" ...

JS - Reducing in size increases request size

I'm facing an issue with compressing my request - instead of reducing the size, it seems to be increasing it: const requestData = LZString.compress(JSON.stringify({ data: bigBase64StringHere })); await axios.post("api-endpoint", requestData, ...

Exploring nested JSON objects with Jade and Express

Currently, I'm feeling a bit frustrated as I struggle to find a way to access the "media" content from this specific JSON object using Jade. { "summary":"Jose Mourinho names his Real Madrid side to face Borussia Dortmund in the Champions Lea ...

Issue encountered while attempting to pass a function within the data in React

I've encountered an issue while trying to pass a function called sectionOne and then calling it from another component. The error message I received is quite confusing. Error: Warning: Functions are not valid as a React child. This may happen if you r ...

What is the process for creating a duplicate element within the target div after the original element has been dropped?

Imagine a scenario where the "HI" div is dragged into the "DRAG HERE" div, causing the "HI" div to be removed from its original location. What if, instead of disappearing, dragging the "HI" div to another location generated a new "HI" div? /** * Fu ...

Having trouble terminating the session with the authentication provider SSO on Node JS

I'm struggling with ending the session properly when a user makes a request to my /logout endpoint. I want to clear the session and require the user to log in again via SSO. Currently, after an initial login, I remain logged in without needing to re-e ...

Get a numerical value from a JSON object

Attempting to retrieve information from an API, but encountering an issue due to a numeric property name. The method for accessing the data is as follows: Success: data[i].price_usd Unsuccessful Attempt: data[i].24h_volume_usd Have experimented with ...

What could be causing all of my Bootstrap accordion panels to close at once instead of just the one I selected?

When implementing Bootstrap accordions in my projects, I encountered an issue where closing one accordion would cause the others to also close when reopened. To address this problem, I utilized the collapse and show classes in Bootstrap to enable users to ...

Maximizing Efficiency: Utilizing a Single Panel across Multiple HTML Files with jQueryMobile

Can a panel defined in index.html be used on another page, such as results.html? Or do I have to define the panel on every page and duplicate the HTML code on each page? Because I want the panel to be consistent across all pages. This is the panel in my ...

JavaScript: Creating Custom IDs for Element Generation

I've been developing a jeopardy-style web application and I have a feature where users can create multiple teams with custom names. HTML <!--Score Boards--> <div id="teamBoards"> <div id="teams"> ...

Error: Angular7 Unable to locate namespace 'google'

I am currently utilizing the import { MapsAPILoader } from '@agm/core'; package to enable auto-complete address search functionality. However, I have encountered an error message stating cannot find namespace 'google'. The error occu ...

Thirteen consecutive attempts to fetch information have resulted in failure

I've encountered an issue while attempting to fetch data from my .NET Core 7 API. The error message I'm receiving is as follows: *Unhandled Runtime Error Error: fetch failed Call Stack Object.fetch node:internal/deps/undici/undici (11413:11) pro ...

toggle between utilizing the page object API and the primary Nightwatch API

Currently, I am incorporating the page object model alongside nightwatch for my testing procedures. Encountering challenges while trying to interact with an element led me to execute some jquery. However, the 'execute' command is not featured in ...

Unable to successfully import an external HTML file that contains a script tag

I am currently experiencing an issue with my index.html <!doctype html> <html lang="en"> <head> <meta charset="utf-8> <title>MyApp</title> <link rel="import" href="./common.html"> </head> <body> ...

Setting a variable in Angular after a successful AJAX call

Working on a new small application and experimenting with Angular. Encountering an issue where I am making an AJAX GET request upon clicking a button. After receiving the response, I want to set a variable to hold the result, but for some reason the variab ...

Using JavaScript to open links in a new tab with the target

document.getElementById("mahacareer").onclick = function () { window.open("http://www.mahacareermitra.in", '_blank'); }; <a href="" id="mahacareer">Access the portal</a> Hi there, I am looking to have the link above open in a new tab ...

What could be causing this issue with the ng-bind and ng-show directives not functioning as expected?

Attempting to show data retrieved from Google's Place Service. Oddly enough, the object can be logged to the console within the controller, but the directives in the HTML file remain blank. Since no map is being used, a div element was passed as the n ...

Automating Python functions with Selenium API after exceeding page load time

I am currently working on a Python script that uses Selenium with the Chrome webdriver. Occasionally, the script gets stuck while loading pages because of JavaScript trying to load in the background. I have tried using the line: driver.execute_script("$(w ...

How to automatically close the menu on a Wordpress theme after clicking a link

I am currently using a Wordpress theme named ichiban, which can be viewed by following this link. Within this theme, I have created custom menu items that are designed to link directly to different sections on the same page. However, I am encountering an i ...