0

Here is my code:

var obj ={
    "item":{
        name:"manu",
        nachname:"kostner",
        beruf:"astronaut"
    },
    "item":{
        name:"juliad",
        nachname:"sib",
        beruf:"mönch"
    }    
  };
  alert(obj.item[0].name);

alert(obj.item.name); is working, but I don't want to give every item a new name..

4
  • 7
    Then why don't just use array to contain those objects? Commented Jul 9, 2015 at 9:20
  • have you tried javascript loop? Commented Jul 9, 2015 at 9:21
  • I'd suggest reading up on basic JS data types such as objects vs. arrays. Commented Jul 9, 2015 at 9:22
  • Your object structure is look wrong.There are two object with same keys "item" Commented Jul 9, 2015 at 9:26

1 Answer 1

1

You have to use array, as keys in objects have to be unique:

var arr = [{
      name:"manu",
      nachname:"kostner",
      beruf:"astronaut"
    }, {
      name:"manu",
      nachname:"kostner",
      beruf:"astronaut"
    }];

alert(arr[0].name);
Sign up to request clarification or add additional context in comments.

Comments

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.