简体   繁体   English

如何过滤和循环遍历JavaScript中的嵌套关​​联对象?

[英]How to filter and loop through a nested associative object in JavaScript?

So I have three nested associative objects/arrays in total. 所以我总共有三个嵌套的关联对象/数组。 Employee1 and employee2 were first made, and then employees was created in order to nest the two associative objects. 首先创建Employee1和employee2,然后创建雇员以嵌套两个关联对象。 Next, I created a third associative object, employee3, to nest into the employees object. 接下来,我创建了第三个关联对象employee3,以嵌套到employees对象中。 What I am trying to do now is filter through the 2D array using the .filter() method to only bring up the employees that are currently working there, which is indicated in the arrays by ["isCurrent"] = true; 我现在想做的是使用.filter()方法过滤2D数组,以仅调出当前正在那里工作的员工,这在数组中由[“ isCurrent”] = true表示; but all I'm getting in the console window is undefined. 但是我在控制台窗口中得到的所有内容都是不确定的。 Why is this happening and how do I fix this? 为什么会发生这种情况,我该如何解决?

 var employee1 = []; employee1["id"] = 33; employee1["name"] = "Carey Shanks"; employee1["title"] = "Knife Maker"; employee1["department"] = "fabrication"; employee1["isCurrent"] = true; var employee2 = []; employee2["id"] = 34; employee2["name"] = "Giles Newman"; employee2["title"] = "Lead Sales"; employee2["department"] = "Customer Service"; employee2["isCurrent"] = true; var employees = []; employees[0] = []; employees[0]["id"] = 33; employees[0]["name"] = "Carey Shanks"; employees[0]["title"] = "Knife Maker"; employees[0]["department"] = "fabrication"; employees[0]["isCurrent"] = true; var employee3 = []; employee3["id"] = 35; employee3["name"] = "Tori G."; employee3["title"] = "Product Demonstrator"; employee3["department"] = "Marketing"; employee3["isCurrent"] = false; //MERING THE ARRAYS employees.push(employee2); employees.push(employee3); //TESTING TO SEE IF IT POPULATED //window.console.log(employees); var i; var currentEmployee = function (isCurrent) { var isCurrentEmployee = true; for (i in employees) { if (isCurrent !== true) { isCurrentEmployee = false; break; } } return isCurrentEmployee; }; var isCompanyEmployee = employees.filter(currentEmployee); window.console.log(isCompanyEmployee[i]); 

Some issues: 一些问题:

  • Arrays should only be used with numeric indicies. 数组只能与数字索引一起使用。 If you want to use arbitrary string keys, use an object instead. 如果要使用任意字符串键,请使用一个对象。
  • The first argument to the filter callback is the current item being iterated over; filter回调的第一个参数是要迭代的当前项; your 你的

     var currentEmployee = function (isCurrent) { 

    defines isCurrent as the first argument, but that's not the isCurrent property. isCurrent定义为第一个参数,但这不是isCurrent属性。 If you wanted to immediately extract the isCurrent property from the object, destructure the parameter instead, and then you can just return it: 如果要立即从对象中提取isCurrent属性,请改为isCurrent参数,然后可以将其返回:

     var currentEmployee = function ({ isCurrent }) { return isCurrent; } 

    Or, if you're not comfortable with destructuring, this is equivalent to 或者,如果您不愿意进行销毁,则相当于

     var currentEmployee = function (employee) { return employee.isCurrent; } 
  • i has no meaning outside the currentEmployee function - log the filtered array instead: icurrentEmployee函数之外没有任何意义-而是记录过滤后的数组:

     console.log(isCompanyEmployee); 
  • You never push employee1 to the array. 您永远不会将employee1推送到阵列。

 var employee1 = {}; employee1["id"] = 33; employee1["name"] = "Carey Shanks"; employee1["title"] = "Knife Maker"; employee1["department"] = "fabrication"; employee1["isCurrent"] = true; var employee2 = {}; employee2["id"] = 34; employee2["name"] = "Giles Newman"; employee2["title"] = "Lead Sales"; employee2["department"] = "Customer Service"; employee2["isCurrent"] = true; var employees = []; employees[0] = {}; employees[0]["id"] = 33; employees[0]["name"] = "Carey Shanks"; employees[0]["title"] = "Knife Maker"; employees[0]["department"] = "fabrication"; employees[0]["isCurrent"] = true; var employee3 = {}; employee3["id"] = 35; employee3["name"] = "Tori G."; employee3["title"] = "Product Demonstrator"; employee3["department"] = "Marketing"; employee3["isCurrent"] = false; //MERING THE ARRAYS employees.push(employee1); employees.push(employee2); employees.push(employee3); //TESTING TO SEE IF IT POPULATED //window.console.log(employees); var i; var currentEmployee = function ({ isCurrent }) { return isCurrent; }; var currentEmployees = employees.filter(currentEmployee); window.console.log(currentEmployees); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM