在Javascript中使用Object Literal的Array Literal

时间:2016-11-19 20:21:46

标签: 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() {
	var all;
	for (var i in insurance){
		all.push(insurance[i].id + " " + insurance[i].policy[i].name +  " " + insurance[i].policy[i].cost);
	}
	return all;
}
console.log(loop());




我很难尝试访问属性中的值。 答案应该是

John geico 400
John nationwide 500

并继续,直到它通过每个id和每个政策。 我知道如何使用

来得到答案
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); 

5 个答案:

答案 0 :(得分:0)

您需要将all初始化为数组。

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

并迭代政策。

您可以使用外部Array#reduce和内部返回结果 Array#forEach

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

答案 1 :(得分:0)

试试这个。

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

答案 2 :(得分:0)

您必须迭代外部数组insurance数组和内部policy数组。您可以使用嵌套的Array.prototype.map()将外部对象转换为数组,然后通过应用Array.prototype.concat()将子数组展平为单个数组:

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

如果您使用ES6箭头函数,数组传播,参数解构和模板字符串,或者甚至更短的代码:

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

答案 3 :(得分:0)

希望此解决方案有所帮助:)!



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




答案 4 :(得分: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},
    ]
}
];

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