2

I've been trying to return the first object of the first array in my JSON object, but I'm having some trouble.

I want to return the name of the first "vitamins", which is "Vitamin B2", in an tag.

Here is my JSON object

{
  "nutrients": {
    "vitamins": [{
      "name": "Vitamin B2",
      "current": "1.7"
    },
    {
      "name": "Vitamin B6",
        "current": "10.9"
    }],
      "minerals": [{
        "name": "Zinc",
        "current": "1.7"
    }]
  }
}

And here is my code trying to map over it

import nutrients from '../../vitamins.json';

renderData() {
  return Object.keys(nutrients).map((nutrient, index) => {
    console.log('it works', nutrient[0].name, index);
    return (
      <option value="" key={index}>{nutrient[0].name}</option>
    )
  }
)}
3
  • "nutrients['vitamins'][0].name" this is enough Commented May 22, 2018 at 4:51
  • There are no arrays in JSON Object. Commented May 22, 2018 at 4:56
  • You need to parse that JSON into a real JS object before doing any manipulation on it. Commented May 22, 2018 at 5:13

4 Answers 4

1
var a ={
  "nutrients": {
    "vitamins": [{
      "name": "Vitamin B2",
      "current": "1.7"
    },
    {
      "name": "Vitamin B6",
        "current": "10.9"
    }],
      "minerals": [{
        "name": "Zinc",
        "current": "1.7"
    }]
  }
}

console.log(a.nutrients['vitamins'][0].name)
Sign up to request clarification or add additional context in comments.

2 Comments

Great, this works. Thanks. Now what if I want to return all of the names? If I do console.log(micros.nutrients['vitamins'].name), I get "undefined".
a.nutrients['vitamins'].forEach(item =>{ console.log(item.name) });
0

Assuming the entire object to be in state.data

`render(){
 const {vitamins}=this.state.data.nutrients;
 vitamins.map((item,index)=>{
     if(index === 0)
     {    return (<div>item.name</div>)
     }`
  })
 }`

the output will print Vitamin-B2

4 Comments

Thanks a lot, this helped.
The reason why you wrapped {vitamins} in a curly brace in const {vitamins} is because it's the name of an object, so it has to be wrapped in {}?
@bigjohnjr it is an ES6 shorthand.
const {vitamins} =this.state.data.nutrients when expanded becomes this.state.data.nutrients.vitamins. It's the same thing but it helps in code clarity and ease of understanding.
0

You could use logic something like below:

let nutrientObj = {
  "nutrients": {
    "vitamins": [{
      "name": "Vitamin B2",
      "current": "1.7"
    },
    {
      "name": "Vitamin B6",
        "current": "10.9"
    }],
      "minerals": [{
        "name": "Zinc",
        "current": "1.7"
    }]
  }
}

let nutrients = nutrientObj.nutrients;
let firstElement = nutrients[Object.keys(nutrients)[0]];

console.log(firstElement && firstElement.length ? firstElement[0].name : "")

Comments

0

You may use destructuring in ES6, use this as reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Array_destructuring

import nutrients from '../../vitamins.json'
renderData() {
  const { nutrients: { vitamins: [{ name: vitaminName }] }  } = nutrients; //it destructures the given object, it will directly fetch the vitamin B2.
  console.log("Vitamin Name is = "+vitaminName)
  <option value="">{vitaminName}</option>
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.