1

I am doing a project using React-Redux and am quite new to it. I have a JSON object which looks like this

{
  "topLevel": {
    "
    "Title": "Title Text",
    "Content": "",
    "Categories": [
      "123",
      "456",
      "789",
    ]
  },
"NextLevel": [
    {
      "ID": "123",
      "Name": "ValentinesDay",
      "Type": 0,
      ]
    },
    {
      "ID": "456",
      "Name": "Family Meals",
      "Title": "Family Meals",
    },
 {
      "ID": "999",
      "Name": "sample",
      "Title": "sample",
    },
{
      "ID": "789",
      "Name": "sample",
      "Title": "sample",
    },

I need to find a way to iterate through the TopLevel and find matchingIds in the second one and display those elements on the DOM.

My reducers looks like this now

switch (action.type) {
    case RECEIVE_PRODUCTS:

      return {
        ...state,
        ...action.products.NextLevel.reduce((obj, product) => {

          obj[product.ID] = product;
          return obj;
        }, {})
      }
    default:
      return state

this displays all the elements in the NextLevel object. How do I include a condition that ensures I pickup only IDS that have been listed in First Level?

keerti
  • 245
  • 5
  • 19
  • In the reducer you should add an if-block that only assigns the product to the associative array if the product-ID is contained in the categories array of the top-level. According to [this answer](http://stackoverflow.com/questions/11015469/javascript-check-array-for-value) something like `if (action.products.topLevel.Categories.indexOf(product.ID) > -1) { obj[product.ID] = product; } return obj;` inside the reducer should work for non-ancient browsers (though I'm not a javascript expert) – Roman Vottner May 03 '17 at 16:38
  • that really helped. Thanks – keerti May 04 '17 at 15:26

0 Answers0