2016-11-19 13 views
0

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); 

Antwort

0

Sie haben die äußereen Anordnung insurance Array und den inneren policy Array iterieren. Sie können die äußeren Objekte umwandeln verschachtelte Array.prototype.map() auf Arrays verwendet wird, und dann abflachen die Subarrays auf einen einzigen Array von Array.prototype.concat() Anwendung:

function loop(arr) { 
 
    return [].concat.apply([], arr.map(function(item) { 
 
    return item.policy.map(function(policy) { 
 
     return item.id + " " + policy.name + " " + policy.cost; 
 
    }); 
 
    })); 
 
} 
 

 
var insurance = [ 
 
    { 
 
    id: 'John', 
 
    policy: [ 
 
     {name: 'geico', cost: 400}, 
 
     {name: 'nationwide', cost: 500}, 
 
     {name: 'maine', cost: 550}, 
 
     {name: 'litty', cost: 450} 
 
    ] 
 
    }, 
 
    { 
 
    id: 'Chris', 
 
    policy: [ 
 
     {name: 'emran', cost: 400}, 
 
     {name: 'kite', cost: 500}, 
 
     {name: 'tile log', cost: 450}, 
 
     {name: 'seatle ins', cost: 600}, 
 
    ] 
 
    } 
 
]; 
 

 
console.log(loop(insurance));

Oder einen noch kürzeren Code, wenn Sie ES6 Pfeil-Funktionen verwenden , array Aufstrich, Parameter Destrukturierung und template string:

const loop = (arr) => [].concat(...arr.map(({ id, policy }) => 
 
    policy.map(({ name, cost }) => `${id} ${name} ${cost}`) 
 
)); 
 

 
const insurance = [ 
 
    { 
 
    id: 'John', 
 
    policy: [ 
 
     {name: 'geico', cost: 400}, 
 
     {name: 'nationwide', cost: 500}, 
 
     {name: 'maine', cost: 550}, 
 
     {name: 'litty', cost: 450} 
 
    ] 
 
    }, 
 
    { 
 
    id: 'Chris', 
 
    policy: [ 
 
     {name: 'emran', cost: 400}, 
 
     {name: 'kite', cost: 500}, 
 
     {name: 'tile log', cost: 450}, 
 
     {name: 'seatle ins', cost: 600}, 
 
    ] 
 
    } 
 
]; 
 

 
console.log(loop(insurance));

0

verwenden, müssen Sie all als Array initialisieren.

var all = []; 
    //  ^^^^ 

Und um Politik auch zu iterieren.

Sie können das Ergebnis mit einem äußeren Array#reduce und einem inneren Array#forEach zurückgeben.

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() { 
 
    return insurance.reduce(function (r, a) { 
 
     return r.concat(a.policy.map(function (b) { 
 
      return a.id + " " + b.name + " " + b.cost; 
 
     })); 
 
    }, []); 
 
} 
 

 
console.log(loop());

0

versuchen diese.

insurance.forEach(function(item, index) { 
    Id = item.id; 
    item.policy.forEach(function(item1, index1) { 
    console.log(Id + " " + item1.name + " " + item1.cost) 
    }) 
}); 
0

Hoffe diese Lösung hilft :)!

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() { 
 
    
 
var info1; 
 
var into2; 
 
var all = [] 
 
for(var i in insurance){ 
 
    var user = insurance[0].id; 
 
    //console.log(user) 
 
    for(var j in insurance[0].policy){ 
 
     info1 = user + " "+ insurance[0].policy[0].name + " " + insurance[0].policy[0].cost; 
 
     info2 = user + " "+ insurance[0].policy[1].name + " " + insurance[0].policy[1].cost; 
 
    } 
 
} 
 
all.push(info1, info2); 
 

 
return all; 
 
} 
 

 
console.log(loop());

0

var insurance = [ 
 
    { 
 
    id: 'John', 
 
    policy: [ 
 
     {name: 'geico', cost: 400}, 
 
     {name: 'nationwide', cost: 500}, 
 
     {name: 'maine', cost: 550}, 
 
     {name: 'litty', cost: 450} 
 
    ] 
 
    }, 
 
    { 
 
    id: 'Chris', 
 
    policy: [ 
 
     {name: 'emran', cost: 400}, 
 
     {name: 'kite', cost: 500}, 
 
     {name: 'tile log', cost: 450}, 
 
     {name: 'seatle ins', cost: 600}, 
 
    ] 
 
} 
 
]; 
 

 
\t \t 
 
insurance.map(function(b){ 
 
    return b.policy.map(function(a){console.log(b.id,a.name,a.cost);}); 
 
    }); 
 

Verwandte Themen