2017-09-05 118 views
-1

我有對象的數組的數組,看起來像這樣:如何通過對象屬性的迭代一個數組,在對象

data = [ 
    { 
    title: 'John Doe', 
    departments: [ 
     { name: 'Marketing', slug: 'marketing'}, 
     { name: 'Sales', slug: 'sales'}, 
     { name: 'Administration', slug: 'administration'}, 
    ] 
    }, 
    { 
    title: 'John Doe Junior', 
    departments: [ 
     { name: 'Operations', slug: 'operations'}, 
     { name: 'Sales', slug: 'sales'}, 
    ] 
    }, 
    { 
    title: 'Rick Stone', 
    departments: [ 
     { name: 'Operations', slug: 'operations'}, 
     { name: 'Marketing', slug: 'marketin'}, 
    ] 
    }, 
] 

我如何可以遍歷每個對象的部門數組,並創建新的陣列在那裏我會按部門分類的員工,從而使最終的結果會是這樣的:

operations = [ 
    { 
    title: 'John Doe Junior', 
    departments: [ 
     { name: 'Operations', slug: 'operations'}, 
     { name: 'Sales', slug: 'sales'}, 
    ] 
    }, 
    { 
    title: 'Rick Stone', 
    departments: [ 
     { name: 'Operations', slug: 'operations'}, 
     { name: 'Marketing', slug: 'marketin'}, 
    ] 
    }, 
] 

marketing = [ 
    { 
    title: 'John Doe', 
    departments: [ 
     { name: 'Marketing', slug: 'marketing'}, 
     { name: 'Sales', slug: 'sales'}, 
     { name: 'Administration', slug: 'administration'}, 
    ] 
    }, 
    { 
    title: 'Rick Stone', 
    departments: [ 
     { name: 'Operations', slug: 'operations'}, 
     { name: 'Marketing', slug: 'marketin'}, 
    ] 
    }, 
] 

什麼是動態地創建這種陣列的方法是什麼?

更新

我試圖想出了使用從答案,在那裏我會動態地創建部門對象的數組,將有員工組成的數組建議的解決方案:

const isInDepartment = departmentToCheck => employer => employer.departments.find(department => department.slug == departmentToCheck); 

var departments = []; 
function check(departments, name) { 
    return departments.some(object => name === object.department); 
} 

employees.forEach((employee) => { 
    employee.departments.forEach((department) => { 
     let found = check(departments, department.slug); 
     if (!found) { 
      departments.push({ department: department.slug }); 
     } 
     }); 
}); 

departments.forEach((department) => { 
    // push an array of employees to each department 
    //employees.filter(isInDepartment(department)); 
}); 

但是,我不知道如何將員工數組推到數組中的最後一個循環中的對象? 這是fiddle

+0

你嘗試過什麼?您需要使用['Object'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#Methods_of_the_Object_constructor)和['Array'](https:// developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Methods_2)方法。 – Xufox

+0

@ T.J.Crowder我編輯了問題並修復了語法 – Leff

+0

這是什麼類型的語法?你可以使用console.log來輸出一個乾淨的json變量表示形式,如下所示:console.log(JSON.stringify(someVar)); – Ericson578

回答

1

這個怎麼樣?我使用Array.protoype.filter操作,並且使用高階函數(在本例中爲返回函數的函數)創建謂詞(返回布爾值的函數),以檢查員工是否在特定部門。我在代碼中添加了一些(希望)澄清評論。

編輯:使用您提供的新代碼和上下文this JSFiddle demo顯示它將如何協同工作。

const employees = [ 
 
    { 
 
    title: 'John Doe', 
 
    departments: [ 
 
     { name: 'Marketing', slug: 'marketing'}, 
 
     { name: 'Sales', slug: 'sales'}, 
 
     { name: 'Administration', slug: 'administration'} 
 
    ] 
 
    }, 
 
    { 
 
    title: 'John Doe Junior', 
 
    departments: [ 
 
     { name: 'Operations', slug: 'operations'}, 
 
     { name: 'Sales', slug: 'sales'} 
 
    ] 
 
    }, 
 
    \t { 
 
    title: 'Rick Stone', 
 
    departments: [ 
 
     { name: 'Operations', slug: 'operations'}, 
 
     { name: 'Marketing', slug: 'marketin'} 
 
    ] 
 
    } 
 
]; 
 

 
// given a department, this returns a function that checks 
 
// whether an employee is in the specified department 
 
// NOTE: the "find" returns the found object (truthy) 
 
// or undefined (falsy) if no match was found. 
 
const isInDepartment = 
 
\t departmentToCheck => 
 
    \t employee => employee.departments.find(dep => dep.name == departmentToCheck); 
 

 
const employeesInMarketing = employees.filter(isInDepartment('Marketing')); 
 
const employeesInOperations = employees.filter(isInDepartment('Operations')); 
 

 
console.log('Employees in marketing', employeesInMarketing); 
 
console.log('Employees in operations', employeesInOperations);

+0

我不明白這個downvoting的原因,它可以用更多的解釋,但是這不是什麼理由來downvote它 –

+0

謝謝,那是一個非常快(即時據我所知),所以我開始想知道,當你回答一個帶有負分的問題時它是否會自動降級?(不是以前任何這樣的規則的頭,但它會使*一些*感覺)。Btw。我在代碼中添加了一些評論,你認爲還有更多的解釋空間嗎?如果是,在哪裏? – jonahe

+1

非常感謝您的建議,我用您的代碼更新了我的問題,我試圖動態創建一個帶有部門對象的數組,然後檢查員工,但出於某種原因,我沒有獲得明確的值我的功能找到了。 – Leff