如何在对象的对象数组中映射这组对象

时间:2018-06-01 04:35:55

标签: javascript

我想要cooked

的值
 var kvArray = [
 {key: 1, value: [{items: 10, price: {raw: 10, cooked: 20}}, {items: 20, price: {raw: 20, cooked: 60}}, {items: 30, price: {raw: 30, cooked: 90}}, {items: 40, price: {raw: 50, cooked: 70}}, {items: 50, price: {raw: 50, cooked: 90}}]}, 
 {key: 2, value: [{items: 40, price: {raw: 10, cooked: 20}}, {items: 30, price: {raw: 10, cooked: 70}}, {items: 20, price: {raw: 20, cooked: 20}}, {items: 10, price: {raw: 20, cooked: 20}}, {items: 60, price: {raw: 60, cooked: 20}}]}, 
 {key: 3, value: [{items: 70, price: {raw: 60, cooked: 20}}, {items: 60, price: {raw: 10, cooked: 70}}, {items: 50, price: {raw: 10, cooked: 60}}, {items: 30, price: {raw: 30, cooked: 20}}, {items: 20, price: {raw: 10, cooked: 80}}]}
 ];


 kvArray.map((kvA , index) =>{
   console.log(kvA.value)
 } )

2 个答案:

答案 0 :(得分:1)

您需要另一个map()



var kvArray = [
 {key: 1, value: [{items: 10, price: {raw: 10, cooked: 20}}, {items: 20, price: {raw: 20, cooked: 60}}, {items: 30, price: {raw: 30, cooked: 90}}, {items: 40, price: {raw: 50, cooked: 70}}, {items: 50, price: {raw: 50, cooked: 90}}]}, 
 {key: 2, value: [{items: 40, price: {raw: 10, cooked: 20}}, {items: 30, price: {raw: 10, cooked: 70}}, {items: 20, price: {raw: 20, cooked: 20}}, {items: 10, price: {raw: 20, cooked: 20}}, {items: 60, price: {raw: 60, cooked: 20}}]}, 
 {key: 3, value: [{items: 70, price: {raw: 60, cooked: 20}}, {items: 60, price: {raw: 10, cooked: 70}}, {items: 50, price: {raw: 10, cooked: 60}}, {items: 30, price: {raw: 30, cooked: 20}}, {items: 20, price: {raw: 10, cooked: 80}}]}
];


var cookedArr = kvArray.map((kvA, index) =>{
 return kvA.value.map(p => p.price.cooked)
});
console.log(cookedArr);




OR:如果您不需要返回任何内容,请使用forEach()



var kvArray = [
 {key: 1, value: [{items: 10, price: {raw: 10, cooked: 20}}, {items: 20, price: {raw: 20, cooked: 60}}, {items: 30, price: {raw: 30, cooked: 90}}, {items: 40, price: {raw: 50, cooked: 70}}, {items: 50, price: {raw: 50, cooked: 90}}]}, 
 {key: 2, value: [{items: 40, price: {raw: 10, cooked: 20}}, {items: 30, price: {raw: 10, cooked: 70}}, {items: 20, price: {raw: 20, cooked: 20}}, {items: 10, price: {raw: 20, cooked: 20}}, {items: 60, price: {raw: 60, cooked: 20}}]}, 
 {key: 3, value: [{items: 70, price: {raw: 60, cooked: 20}}, {items: 60, price: {raw: 10, cooked: 70}}, {items: 50, price: {raw: 10, cooked: 60}}, {items: 30, price: {raw: 30, cooked: 20}}, {items: 20, price: {raw: 10, cooked: 80}}]}
];


kvArray.forEach((kvA, index) =>{
 kvA.value.forEach( (p, i) => console.log(p.price.cooked));
});




答案 1 :(得分:1)

我会缩小然后映射哪些将返回平面阵列上的所有熟食值。

var kvArray = [
 {key: 1, value: [{items: 10, price: {raw: 10, cooked: 20}}, {items: 20, price: {raw: 20, cooked: 60}}, {items: 30, price: {raw: 30, cooked: 90}}, {items: 40, price: {raw: 50, cooked: 70}}, {items: 50, price: {raw: 50, cooked: 90}}]}, 
 {key: 2, value: [{items: 40, price: {raw: 10, cooked: 20}}, {items: 30, price: {raw: 10, cooked: 70}}, {items: 20, price: {raw: 20, cooked: 20}}, {items: 10, price: {raw: 20, cooked: 20}}, {items: 60, price: {raw: 60, cooked: 20}}]}, 
 {key: 3, value: [{items: 70, price: {raw: 60, cooked: 20}}, {items: 60, price: {raw: 10, cooked: 70}}, {items: 50, price: {raw: 10, cooked: 60}}, {items: 30, price: {raw: 30, cooked: 20}}, {items: 20, price: {raw: 10, cooked: 80}}]}
 ];
console.log(
  kvArray.reduce(
    (arr, elem) => arr.concat(elem.value), []
  ).map(a=>a.price.cooked)
)