为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

30秒到达战场 2023-01-06 15:45:20
第一次在这里发帖,希望得到一些帮助,我似乎无法弄清楚如何解决这个问题。它基本上是创建一个函数来接收返回新对象的对象数组。由于某种原因,推送不会通过并返回推送的错误属性未定义。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"]}感谢你们提供的任何建议或提示
查看完整描述

3 回答

?
蝴蝶不菲

TA贡献1810条经验 获得超4个赞

看起来您对遍历数组与键入对象有点困惑。


let organizeInstructors = function(instructors) {

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

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

    const instructor = instructors[i]

    if(!output[instructor.course]) { 

      output[instructor.course] = []

    }                                                   

    output[instructor.course].push(instructor.name)   

   }

return output;

}

console.log(organizeInstructors([

  {name: "Samuel", course: "iOS"},

  {name: "Victoria", course: "Web"},

  {name: "Karim", course: "Web"},

  {name: "Donald", course: "Web"}

]))

添加 constinstructor也使其更易于阅读


查看完整回答
反对 回复 2023-01-06
?
白衣非少年

TA贡献1155条经验 获得超0个赞

使用reduce


data = [ { name: "Samuel", course: "iOS" }, { name: "Victoria", course: "Web" }, { name: "Karim", course: "Web" }, { name: "Donald", course: "Web" }, ];

getObj = (data) =>

  data.reduce(

    (r, c) => (

      !r[c.course] // checks if accumulator doesn't have c.course as key  

        ? ((r[c.course] = []), r[c.course].push(c.name)) // then make an array that corresponds the key then push c.name

        : r[c.course].push(c.name), // else push c.name to the corresponding array 

      r

    ),

    {}

  );

console.log(getObj(data));


查看完整回答
反对 回复 2023-01-06
?
慕标琳琳

TA贡献1830条经验 获得超9个赞

这使用Array.prototype.reduce方法。

请注意,这不会检查该值是否已存在于课程数组中,只会盲目添加。这可能意味着您在同一课程中获得多个同名实例。

const organizeInstructors = function(instructors) {

  return instructors.reduce((cumulative, current) => {

    // if we don't have a course in cumulative object, add it.

    if (!cumulative[current.course]) cumulative[current.course] = [];

    // add in the current name.

    cumulative[current.course].push(current.name);

    // return the cumulative object for the next iteration.

    return cumulative;

  }, {});

}

console.log(organizeInstructors([{

    name: "Samuel",

    course: "iOS"

  },

  {

    name: "Victoria",

    course: "Web"

  },

  {

    name: "Karim",

    course: "Web"

  },

  {

    name: "Donald",

    course: "Web"

  }

]));


查看完整回答
反对 回复 2023-01-06
  • 3 回答
  • 0 关注
  • 181 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信