0

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"]}

3 Answers 3

1

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)

Sign up to request clarification or add additional context in comments.

Comments

1

You can use Array.prototype.reduce() and Array.prototype.map() to do:

// 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

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.