1

My data look like this:

enter image description here

and my code looks like this:

    const navItemData = props.data.navigation_bar;
    let temporaryDrawers = [];

    if (navItemData) {
        for (let i = 0; i < navItemData.length; i++) {
            if (navItemData[i].object_type === 'temporary_drawer') {
                temporaryDrawers.push(navItemData[i]);

                // CODE HERE
            }
        }
    }

This is the log of my temporaryDrawers array:

enter image description here

I want to say in the //CODE HERE that:

if (navigation_bar.content === data.key with the same name e.g. 'products')
then temporaryDrawers.push the array 'products'

and the result that I'm expecting is to get a temporaryDrawers array that looks like this:

enter image description here

This is my data structure:

{
  "website": {
    "navigation_bar": [
      {
        "object_type": "temporary_drawer",
        "title": "Products",
        "content": "products"
      },
      {
        "object_type": "temporary_drawer",
        "title": "Resources",
        "content": "resources"
      },
      {
        "object_type": "navigation_button",
        "title": "Pricing",
        "link": "/pricing"
      }
    ],
    "products": [
      {
        "name": "Lorem ipsum",
        "description": "Lorem ipsum dolor sit amet",
        "image": "/mockImage1.png"
      },
      {
        "name": "Lorem ipsum",
        "description": "Lorem ipsum dolor sit amet",
        "image": "/mockImage2.png"
      },
      {
        "name": "Lorem ipsum",
        "description": "Lorem ipsum dolor sit amet",
        "image": "/mockImage3.png"
      },
      {
        "name": "Lorem ipsum dolor",
        "description": "Lorem ipsum dolor sit amet",
        "image": "/mockImage4.png"
      }
    ]
  }
}

How can I do this?

4
  • 3
    Please don't insert screenshots, post real code. As of now, it is impossible to answer you question because we cannot see the complete structure of your data. For example: Where is that .key property defined? It seems it's defined inside the items in products, but that's just guessing. Commented May 18, 2020 at 15:19
  • @David fixed it. Commented May 18, 2020 at 15:31
  • 2
    Please note that JSON refers to a string format of a JavaScript object. It looks like you are just working with JavaScript objects and not their JSON string representation, the JSON tag is not correct. Commented May 18, 2020 at 15:36
  • Your data (at the end) has a toplevel "website" property, which you never use in your code. Commented May 18, 2020 at 15:42

1 Answer 1

4

I think this can help. You are dynamically creating a property that reflects the string under 'content'. Next, you are using the same string as a key in the data object in order to attach a list to the created property

if (navItemData) {
  for (let i = 0; i < navItemData.length; i++) {
    if (navItemData[i].object_type === 'temporary_drawer') {
        const item = navItemData[i];
        temporaryDrawers.push({...item, [item.content]: props.data[item.content]});

    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

And is there a way to rename it to 'content' at the same time?
sure. temporaryDrawers.push({...item, content: props.data[item.content]});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.