javascript - 在 JavaScript 中推送后没有改变数组的长度
问题描述
请帮助我解决奇怪的行为。我声明了一个数组: buildsArray = new Array();
并像这样在数组中添加对象项:
var detailBuild = {
id: elements.data.id,
username: (elements.data.triggered.user) ? elements.data.triggered.user.username : "not user",
project: (elements.data.buildType) ? elements.data.buildType.projectName : "not project",
status: elements.data.status
};
buildsArray.push(detailBuild);
但是推送之后,我的数组的长度属性等于0。虽然
console.log (buildsArray);
console.log ("the length of buildsArray is " + buildsArray.length);
请告诉我我做错了什么,谢谢
完整代码:
return promise.then((elements) => {
console.log (elements.data.build);
elements.data.build.forEach(element => {
var detailpromise = httpService.getBuildDetail(element.href);
detailpromise.then((elements) => {
var detailBuild = {
id: elements.data.id,
username: (elements.data.triggered.user) ? elements.data.triggered.user.username : "not user",
project: (elements.data.buildType) ? elements.data.buildType.projectName : "not project",
status: elements.data.status
};
buildsArray.push(detailBuild);
var item = buildsGroupingByProject.find(element => element.projectName === detailBuild.project);
(item === undefined) ?
buildsGroupingByProject.push({
projectName: detailBuild.project,
buildArrays: new Array(detailBuild)
})
:
item.buildArrays.push(detailBuild);
});
});
console.log (buildsArray);
console.log ("the length of buildsArray is " + buildsArray.length);
return buildsGroupingByProject;
});
}
解决方案
推荐阅读
- kotlin - 如何使 Kotlin lambda 在泛型类中工作
- java - 使用 Spotify docker-maven-plugin 将 docker 镜像推送到 AWS ECR
- python - spacy-自定义标记化不适用于 POS 和 ORTH 和 OP 参数
- angular - 开放层以英里和反向计算范围的长度和宽度
- javascript - 将提取的文本返回到另一个 javascript 文件
- php - 当我将数组放入 foreach 循环时,我不断收到警告
- c++ - C++ 代码中的指针和 AVL 树存在问题
- batch-file - 指定要通过 .bat 文件打开的程序的确切路径
- python - 在 Gitlab 上运行时库的 Pylint E0401 导入错误
- column-oriented - 使用 docker-compose 将 mysql 作为元数据存储添加到 Apache-Druid