亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

遍歷對象數組并使用迭代值創建新對象

遍歷對象數組并使用迭代值創建新對象

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 關注
  • 214 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號