Arrayliteral mit Objektliteral innen Javascript
var insurance = [
{
id: 'John',
policy: [
{name: 'geico', cost: 400},
{name: 'nationwide', cost: 500},
{name: 'maine', cost: 550},
{name: 'litty', cost: 450}
]
},
{
name: 'Chris',
policy: [
{name: 'emran', cost: 400},
{name: 'kite', cost: 500},
{name: 'tile log', cost: 450},
{name: 'seatle ins', cost: 600},
]
}
];
function loop() {
\t var all;
\t for (var i in insurance){
\t \t all.push(insurance[i].id + " " + insurance[i].policy[i].name + " " + insurance[i].policy[i].cost);
\t }
\t return all;
}
console.log(loop());
Ich stecken versuchen, Zugriff auf Werte von Eigenschaften zu erhalten. Antwort sollte wie
John geico 400
John nationwide 500
sein und weiter, bis es durch jede ID und jede Politik geht. Ich weiß, wie man die Antwort bekommen von
console.log(insurance[0].id + " " + insurance[0].policy[0].name + " " + insurance[0].policy[0].cost);
console.log(insurance[0].id + " " + insurance[0].policy[1].name + " " + insurance[0].policy[1].cost);