0

I have something like this

{
    "title": "JSON",
    "content": [
      {
        "item": "23",
        "url":"ug.coml"
        
      },  
      {
        "item": "33",
        "url":"tewts.com"
      }
    ]
  }

I would like to know if I can pass a string to the map line of code.

const pathstring=this.props.path; //this is equal to the string "content"
const block = skillSetData.pathstring.map ((data, index) => {
     return (
    <div>
    <h3>{data.url}</h3>
    </div>
  )
 })
Jacklyn N
  • 77
  • 1
  • 12
  • 1
    `skillSetData[pathstring].map` perhaps? – Nick Mar 08 '23 at 00:21
  • 2
    Does this answer your question? [Accessing an object property with a dynamically-computed name](https://stackoverflow.com/questions/4244896/accessing-an-object-property-with-a-dynamically-computed-name) – Nick Mar 08 '23 at 00:22
  • Please read the usage description of the `json` tag. – trincot Mar 08 '23 at 07:04

1 Answers1

0

You can use the get method from Lodash and navigate using a string path

const data = {
    "title": "JSON",
    "content": [
      {
        "item": "23",
        "url":"ug.coml"
        
      },  
      {
        "item": "33",
        "url":"tewts.com"
      }
    ]
  }

  _.get(data, 'content[0]'); // returns  {"item": "23","url":"ug.coml"}
  _.get(data, 'content[0].item'); // returns 23 
  

For more details https://lodash.com/docs/4.17.15#get

Fateh Mohamed
  • 20,445
  • 5
  • 43
  • 52