skip to Main Content

I am trying to console.log the typeOne(or any other type) property of each object in the donuts array, but I keep getting undefined as the output. My editor is showing that the problem is at the colon character in the object literals, suggesting that I should print a comma instead. However, I believe that is incorrect. What is the issue here and how can I fix it?

var donuts = [
  { typeOne: "Jelly", cost: 1.22 },
  { typeTwo: "Chocolate", cost: 2.45 },
  { typeThree: "Cider", cost: 1.59 },
  { typeFour: "Boston Cream", cost: 5.99 }
];
console.log(donuts.typeOne);
//or
console.log(donuts["typeOne"]); //both console.log shows undefined

4

Answers


  1. In your example donuts is an array not an object (it’s an array of objects). To print the first item, you have to write donuts[0].typeOne. You are getting undefined because you are treating the donuts variable as an object instead of an array.

    Hope it helps!

    Login or Signup to reply.
  2. Because donuts is an array. You can reference items in an array by their indexes.

    For example:

    var donuts = [
      { typeOne: "Jelly", cost: 1.22 },
      { typeTwo: "Chocolate", cost: 2.45 },
      { typeThree: "Cider", cost: 1.59 },
      { typeFour: "Boston Cream", cost: 5.99 }
    ];
    
    console.log(donuts[0].typeOne);
    //or
    console.log(donuts[1]["typeTwo"]);

    You could make things are little more interesting if you change type<Number> to name and do something like this:

    var donuts = [
      { name: "Jelly", cost: 1.22 },
      { name: "Chocolate", cost: 2.45 },
      { name: "Cider", cost: 1.59 },
      { name: "Boston Cream", cost: 5.99 }
    ];
        
    console.log(donuts.find(d => d.name == "Chocolate").cost);
    //or
    console.log(donuts.filter(d => d.cost < 2.0 ));
    Login or Signup to reply.
  3. You are trying to access the property typeOne directly from donuts. But donuts is an array of objects, not a direct object. If you want to get the property typeOne from each object in your array, you need to go at each item with some method like map:

    var result = donuts.map((donut) => donut.typeOne)
    

    Problem is, the property typeOne only exists in the first object of your array, so result here will be ["Jelly", undefined, undefined, undefined]

    To list all types from each object in your array I suggest this:

    var donuts = [
      { type: "Jelly", cost: 1.22 },
      { type: "Chocolate", cost: 2.45 },
      { type: "Cider", cost: 1.59 },
      { type: "Boston Cream", cost: 5.99 }
    ];
    
    var result = donuts.map((donut) => donut.type)
    

    In this case, type is a property in every object, so when you use map you iterate through all the objects in your array and get the value of the property type.

    Hope it helps

    Login or Signup to reply.
  4. So your object "typeOne", is an an array.
    You have to access the object in the array, then the objects properties.

    
    
    //you declare and object like this
    var obj = {
       key: 'value'
    };
    console.log(obj.key);
    
    //you declare a array like this
    var array = [
        'val1',
        'val2'
    ];
    //to access a arrays value you have to use its index
    console.log(array[0])//index starts at zero in javascipt, not one
    
    //so if you have a object in a array, like here
    var donuts = [
      { typeOne: &quot;Jelly&quot;, cost: 1.22 },
      { typeTwo: &quot;Chocolate&quot;, cost: 2.45 },
      { typeThree: &quot;Cider&quot;, cost: 1.59 },
      { typeFour: &quot;Boston Cream&quot;, cost: 5.99 
    ]
    //you first have to, you first have to get the object using by its index
    console.log(donuts[0])//the arrays firsts value: {typeOne etc)
    
    //so now to access the arrays object properties, just get the object from the array first like above, then the properties
    console.log(donuts[0].typeOne)
    
    //or you can get the second object
    console.log(donuts[1].typeTwo)
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search