我想要构建一个excel uploader应用程序。现在,我可以从excel文件中获取数据并将其发送到API来存储信息。
但是我还需要其他东西,并且要将每个值作为excel的id,这是我先保存后得到的。
这是我在存储之前获取excel数据的方式:
$scope.loadWorksheet = function(e){
var file = e.target.result;
var workbook = XLSX.read(file, { type: "binary" });
var sheetName = workbook.SheetNames[0];
$scope.sheet = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]);
console.log($scope.sheet);
$scope.$apply();
};
假设我得到下一个阵列:
[{Name: "Chuck", Age: "30"},
{Name: "Marcus", Age: "18"},
{Name: "Shelly", Age: "29"}]
如何向每条记录添加文档的ID?
每个例子,在保存文档后,我得到了响应id:
$scope.IdDocument = 15;
我需要的是将它放在每条记录上,如下:
[{Name: "Chuck", Age: "30", DocumentId: "15"},
{Name: "Marcus", Age: "18" DocumentId: "15"},
{Name: "Shelly", Age: "29" DocumentId: "15"}]
有办法吗?希望你能帮帮我。
我正在使用AngularJs和Javascript。
答案 0 :(得分:1)
您需要 forEach
,您可以使用angularjs angular.forEach
<强>样本强>
var array = [{Name: "Chuck", Age: "30"},
{Name: "Marcus", Age: "18"},
{Name: "Shelly", Age: "29"}];
array.forEach(function(obj) { obj.DocumentId = "15"; });
console.log(array);
&#13;
答案 1 :(得分:1)
您可以遍历数组并按如下所示添加字段
var people = [{Name: "Chuck", Age: "30"},
{Name: "Marcus", Age: "18"},
{Name: "Shelly", Age: "29"}
];
people.forEach(p => p.DocumentId = "15");
console.log(people)
答案 2 :(得分:1)
使用map
函数。它将返回更新的数组
var oldArray = [{
Name: "Chuck",
Age: "30"
},
{
Name: "Marcus",
Age: "18"
},
{
Name: "Shelly",
Age: "29"
}
];
var newArray = oldArray.map(function(item) {
item.DocumentId = '15';
return item;
})
console.log(newArray)
&#13;
答案 3 :(得分:0)
使用for循环遍历数组对象
var people = [{Name: "Chuck", Age: "30"},
{Name: "Marcus", Age: "18"},
{Name: "Shelly", Age: "29"}];
for(var i=0 ; i<people.length;i++){
people[i].age = "10";
}