在javascript中将多个数据段添加到一行

在javascript中将多个数据段添加到一行,javascript,html,Javascript,Html,我目前正在一个网站上工作,该网站导入JSON数据并在HTML中填充一个表。在本例中,JSON文件已经被解析,并作为JavaScript对象存在 我在遍历对象中的数组以尝试将具有不同键值的数据添加到单个表行时遇到问题 我想要实现的是将在特定部门工作的每个员工的姓名放在一行中 我首先在for循环外部创建一个空数组。 我使用三个单独的for循环遍历了对象,并将员工的姓名附加到这个空数组中。在循环之后,我将包含名称的完整数组设置为空数组。 我一直在做的事情可以在这里看到 常数表= {“雇员”: [{

我目前正在一个网站上工作,该网站导入JSON数据并在HTML中填充一个表。在本例中,JSON文件已经被解析,并作为JavaScript对象存在

我在遍历对象中的数组以尝试将具有不同键值的数据添加到单个表行时遇到问题

我想要实现的是将在特定部门工作的每个员工的姓名放在一行中

我首先在for循环外部创建一个空数组。 我使用三个单独的for循环遍历了对象,并将员工的姓名附加到这个空数组中。在循环之后,我将包含名称的完整数组设置为空数组。 我一直在做的事情可以在这里看到


常数表=
{“雇员”:
[{“开始”:“2016年”
“部门”:“工程”
“雇员”:
[{“id”:“a101”,“名字”:“艾伦”,“姓氏”:“阿尔金”}
,{“id”:“a102”,“名字”:“杰夫”,“姓氏”:“基冈”}
] 
} 
,{“开始”:“2016年”
“部门”:“研发”
“雇员”:
[{“id”:“a103”,“名字”:“米歇尔”,“姓氏”:“琼斯”}
,{“id”:“a104”,“名字”:“彼得”,“姓氏”:“史密斯”}
] 
} 
] 
} 
var DepName=[];
var employeeNames=[];
让MyTable=文档
.querySelector(“#id01”)
.appendChild(document.createElement('table'))
对于(让表的开始步骤。员工)
{
对于(让StartDep.员工的员工)
{
对于(让StartDep.员工的员工1){
var name=Employee1.firstname+“”+Employee1.lasname
employeeNames.push(名称)
设nRow=MyTable.insertRow(-1)
,rCell=0
nRow.insertCell(rCell++).textContent=StartDep.Started
nRow.insertCell(rCell++).textContent=StartDep.Department
nRow.insertCell(rCell++).textContent=employeeNames
}员工姓名=[];
}
}
让Rowhead=MyTable.createTHead().insertRow(-1)
'已开始,部门,名称'。拆分(',')
.forEach((T,i)=>Rowhead.insertCell(i).textContent=T)

使用此代码我期望得到的结果与此类似

console.log(表)

const abc=[];

对于(var j=0;jHi谢谢你,我应该在哪里将这些数据附加到我的表中呢?这里的表与图中的完全一样,我在jsfdler中获取了您的代码,并对您的for循环进行了更改,仅此而已
<div id="id01"></div>
<script>
const table =
  { "Employees": 
    [ { "Started"   : "2016"
      , "Department": "Engineering"
      , "Employee": 
        [ { "id": "a101", "firstname": "Alan",  "surname": "Arkin"  } 
        , { "id": "a102", "firstname": "Geoff", "surname": "keegan" } 
        ] 
      } 
    , { "Started"   : "2016"
      , "Department": "R&D"
      , "Employee": 
        [ { "id": "a103", "firstname": "Michele", "surname": "Jones" } 
        , { "id": "a104", "firstname": "Peter",   "surname": "Smith" } 
        ] 
      } 
    ] 
  } 
var DepName =[];
var employeeNames =[];
let MyTable = document
                .querySelector('#id01')
                .appendChild(document.createElement('table'))

for (let StartDep of table.Employees)
  {
  for (let Employee of StartDep.Employee )
    {
    for (let Employee1 of StartDep.Employee ){
        var name = Employee1.firstname + " " + Employee1.surname
      employeeNames.push(name)
    let nRow = MyTable.insertRow(-1)
      , rCell  = 0
    nRow.insertCell(rCell++).textContent = StartDep.Started
    nRow.insertCell(rCell++).textContent = StartDep.Department
    nRow.insertCell(rCell++).textContent = employeeNames

     }employeeNames=[];
    }
  }

let Rowhead = MyTable.createTHead().insertRow(-1)

'Started,Department,Name(s)'.split(',')
  .forEach((T,i)=>Rowhead.insertCell(i).textContent=T)
</script>
const abc = [];
for(var j=0;j<2;j++)
{
  const gh = [];
  for(var i =0;i<2;i++)
  {
    let name = table.Employees[j].Employee[i].firstname+" "+table.Employees[j].Employee[i].surname
    gh.push(name);
  }
  var totalName = gh.join();
  abc.push(totalName);
}

console.log(table.Employees[0].Employee[0].firstname);
console.log(abc);