遍历对象数组并使用迭代值创建新对象

第一次在这里发帖,希望得到一些帮助,我似乎无法弄清楚如何解决这个问题。它基本上是创建一个函数来接收返回新对象的对象数组。


由于某种原因,推送不会通过并返回推送的错误属性未定义。


const organizeInstructors = function(instructors) {


  let output = {}; // so the obvious which is to create the object

  for (let i = 0; i < instructors.length; i++) {

    if (!output[instructors[course]]) {

      output[instructors[course]] = instructors[course];

    } else {

      output[instructors[course]].push(instructors[name]);

    }

  }

  return output;

};


console.log(organizeInstructors([{

    name: "Samuel",

    course: "iOS"

  },

  {

    name: "Victoria",

    course: "Web"

  },

  {

    name: "Karim",

    course: "Web"

  },

  {

    name: "Donald",

    course: "Web"

  }

]));


预期产出


{

  iOS: ["Samuel"],

  Web: ["Victoria", "Karim", "Donald"]

}

感谢你们提供的任何建议或提示


30秒到达战场
浏览 182回答 3
3回答

蝴蝶不菲

看起来您对遍历数组与键入对象有点困惑。let organizeInstructors = function(instructors) {let output = {}; // so the obvious which is to create the object&nbsp; for(let i = 0; i < instructors.length; i++) {&nbsp;&nbsp; &nbsp; const instructor = instructors[i]&nbsp; &nbsp; if(!output[instructor.course]) {&nbsp;&nbsp; &nbsp; &nbsp; output[instructor.course] = []&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; output[instructor.course].push(instructor.name)&nbsp; &nbsp;&nbsp; &nbsp;}return output;}console.log(organizeInstructors([&nbsp; {name: "Samuel", course: "iOS"},&nbsp; {name: "Victoria", course: "Web"},&nbsp; {name: "Karim", course: "Web"},&nbsp; {name: "Donald", course: "Web"}]))添加 constinstructor也使其更易于阅读

白衣非少年

使用reducedata = [ { name: "Samuel", course: "iOS" }, { name: "Victoria", course: "Web" }, { name: "Karim", course: "Web" }, { name: "Donald", course: "Web" }, ];getObj = (data) =>&nbsp; data.reduce(&nbsp; &nbsp; (r, c) => (&nbsp; &nbsp; &nbsp; !r[c.course] // checks if accumulator doesn't have c.course as key&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ? ((r[c.course] = []), r[c.course].push(c.name)) // then make an array that corresponds the key then push c.name&nbsp; &nbsp; &nbsp; &nbsp; : r[c.course].push(c.name), // else push c.name to the corresponding array&nbsp;&nbsp; &nbsp; &nbsp; r&nbsp; &nbsp; ),&nbsp; &nbsp; {}&nbsp; );console.log(getObj(data));

慕标琳琳

这使用Array.prototype.reduce方法。请注意,这不会检查该值是否已存在于课程数组中,只会盲目添加。这可能意味着您在同一课程中获得多个同名实例。const organizeInstructors = function(instructors) {&nbsp; return instructors.reduce((cumulative, current) => {&nbsp; &nbsp; // if we don't have a course in cumulative object, add it.&nbsp; &nbsp; if (!cumulative[current.course]) cumulative[current.course] = [];&nbsp; &nbsp; // add in the current name.&nbsp; &nbsp; cumulative[current.course].push(current.name);&nbsp; &nbsp; // return the cumulative object for the next iteration.&nbsp; &nbsp; return cumulative;&nbsp; }, {});}console.log(organizeInstructors([{&nbsp; &nbsp; name: "Samuel",&nbsp; &nbsp; course: "iOS"&nbsp; },&nbsp; {&nbsp; &nbsp; name: "Victoria",&nbsp; &nbsp; course: "Web"&nbsp; },&nbsp; {&nbsp; &nbsp; name: "Karim",&nbsp; &nbsp; course: "Web"&nbsp; },&nbsp; {&nbsp; &nbsp; name: "Donald",&nbsp; &nbsp; course: "Web"&nbsp; }]));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript