简体   繁体   English

如何在对象内打印Javascript数据数组

[英]How to print Javascript data Array inside object

I have details of the students;我有学生的详细信息; how to get my expected output?如何获得我的预期输出? In one object I want the name of the student as key and an array of their car names as value在一个对象中,我希望学生的姓名作为键,并将他们的汽车名称数组作为值

let empArray = [{
    "name": "soma",
    "cars Details": [{
      "name": "audi",
      "cost": 120000,
      "Speed": "150Km/hr"
    }, {
      "name": "toyota",
      "cost": 120000,
      "Speed": "150Km/hr"
    }],
    "salary": 40000
  },
  {
    "name": "steven",
    "cars Details": [{
      "name": "bmw",
      "cost": 120000,
      "Speed": "150Km/hr"
    }],
    "salary": 70000
  },
  {
    "name": "booth",
    "cars Details": [{
      "name": "swift",
      "cost": 120000,
      "Speed": "150Km/hr"
    }, {
      "name": "audi",
      "cost": 120000,
      "Speed": "150Km/hr"
    }],
    "salary": 35000
  },
]

let a = {}
var ids = [];
for (let i = 0; i < empArray.length; i++) {
  a = empArray[i]["cars Details"]
  console.log(a)
}

Expected Output预期产出

{soma :["audi","toyota"],steven:["bmw"],booth:["swift","audi"]}

 let empArray = [{ "name": "soma", "cars Details": [{ "name": "audi", "cost": 120000, "Speed": "150Km/hr" }, { "name": "toyota", "cost": 120000, "Speed": "150Km/hr" }], "salary": 40000 }, { "name": "steven", "cars Details": [{ "name": "bmw", "cost": 120000, "Speed": "150Km/hr" }], "salary": 70000 }, { "name": "booth", "cars Details": [{ "name": "swift", "cost": 120000, "Speed": "150Km/hr" }, { "name": "audi", "cost": 120000, "Speed": "150Km/hr" }], "salary": 35000 },] let dict = {} empArray.forEach((e) => { dict[e.name] = e["cars Details"].map((car) => { return car.name }) }) console.log(dict)

You can use Array.prototype.reduce() and Array.prototype.map() to do:你可以使用Array.prototype.reduce()Array.prototype.map()来做:

 // input let empArray = [{ "name": "soma", "cars Details": [{ "name": "audi", "cost": 120000, "Speed": "150Km/hr" }, { "name": "toyota", "cost": 120000, "Speed": "150Km/hr" }], "salary": 40000 }, { "name": "steven", "cars Details": [{ "name": "bmw", "cost": 120000, "Speed": "150Km/hr" }], "salary": 70000 }, { "name": "booth", "cars Details": [{ "name": "swift", "cost": 120000, "Speed": "150Km/hr" }, { "name": "audi", "cost": 120000, "Speed": "150Km/hr" }], "salary": 35000 }, ]; const output = empArray.reduce((outObj, item) => { outObj[item.name] = item['cars Details'].map(detail => detail.name); return outObj }, {}); // test console.log(output)

By the way, if you can, I'd recommend avoiding object keys containing spaces and change cars Details to carsDetails顺便说一句,如果可以的话,我会建议避免包含空格和变更对象键cars DetailscarsDetails

 let empArray=[{name:"soma","cars Details":[{name:"audi",cost:12e4,Speed:"150Km/hr"},{name:"toyota",cost:12e4,Speed:"150Km/hr"}],salary:4e4},{name:"steven","cars Details":[{name:"bmw",cost:12e4,Speed:"150Km/hr"}],salary:7e4},{name:"booth","cars Details":[{name:"swift",cost:12e4,Speed:"150Km/hr"},{name:"audi",cost:12e4,Speed:"150Km/hr"}],salary:35e3}]; let result = empArray.reduce((acc,e) => ({[e.name]:e['cars Details'].map(n => n.name),...acc}), {} ) console.log(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM