I have a slight problem. I am working on an angular application and at some point i am getting some data from an api.
我有一个小问题。我正在开发一个角度应用程序,在某些时候我从api获取一些数据。
I have an existing object with project urls and am getting an array of timeslip objects that have an hours property and a project urls property themselves to indicate which project they belong to.
我有一个带有项目URL的现有对象,并且我得到一个具有hours属性和项目urls属性的timeslip对象数组,以指示它们属于哪个项目。
What I am trying to achieve is to iterate through all the timeslips I get in order to transfer the number of hours to the project in the project array that has a matching url property.
我想要实现的是迭代我得到的所有时间片,以便将项目数组中的小时数转移到具有匹配的url属性的项目数组中。
The project array is : $scope.projectsObject
项目数组是:$ scope.projectsObject
My code looks like this:
我的代码如下所示:
for (var i = 0; i < response.data.timeslips.length; i++) {
//SET LOCAL VARIABLE FOR SELECTED TIMESLIP
var timeslip = response.data.timeslips[i];
//ITERATE THROUGH PROJECTS ARRAY
for (var ii = 0; ii < Object.keys($scope.projectsObject).length; ii++) {
//SEE IF TIMESLIP BELONGS TO PROJECT
if ($scope.projectsObject[ii].url == timeslip.url) {
//SEE IF $scope.projectsObject HAS HOURS PROPERTY
if ('hours' in $scope.projectsObject[ii]) {
//IF YES ADD HOURS TO EXISTING AMOUNT
$scope.projectsObject[ii].hours = $scope.projectsObject[ii].hours + timeslip.hours;
} else {
//IF NOT CREATE IT AND ADD HOURS
$scope.projectsObject[ii] = { "hours": timeslip.hours };
}
}
}
}
The problem seems to be with recognising when the hours of a previous timeslip were already added to the project and I don't want to create the hours property from new, but just add the hours of the current timeslip to the hours that were already previously added.
问题似乎在于识别前一个时间片的小时数已经添加到项目中并且我不想从new创建hours属性,而只是将当前时间片的小时数添加到之前已经过的小时数添加。
It would be great if someone could point out my mistake here, as I just can't seem to find it.
如果有人能在这里指出我的错误会很好,因为我似乎无法找到它。
Thanks a lot!
非常感谢!
2 个解决方案
#1
0
I suggest to use a more concise code.
我建议使用更简洁的代码。
What has changed:
发生了什么变化:
- Iteration for
response.data.timeslips
. - Iteration for
$scope.projectsObject
. - Test if property
url
is equal in both objects. - Provide a default value
0
, ifb.hours
is not set. - Conversion of
timeslip.hours
fromString
toNumber
. - Add the values.
- Assign the new value to
b.hours
(and leave the rest of object b intact).
迭代的响应.data.timeslips。
$ scope.projectsObject的迭代。
测试两个对象中属性url是否相等。
如果未设置b.hours,则提供默认值0。
将timeslip.hours从String转换为Number。
添加值。
将新值分配给b.hours(并保持对象b的其余部分完整)。
var response = { data: { timeslips: [{ "hours": "7.0", "url": "api.sample.com" }, { "hours": "2.5", "url": "api.sample1.com" }, { "hours": "3.1", "url": "api.sample1.com" }, { "hours": "0.5", "url": "api.sample2.com" }] } },
$scope = { projectsObject: [{ "url": "api.sample1.com" }, { "url": "api.sample.com" }, { "url": "api.sample2.com" }] };
response.data.timeslips.forEach(function (timeslip) {
$scope.projectsObject.forEach(function (b) {
if (timeslip.url === b.url) {
b.hours = (b.hours || 0) + +timeslip.hours;
}
});
});
document.write('<pre>' + JSON.stringify($scope, 0, 4) + '</pre>');
#2
0
Change:
if ($scope.projectsObject[ii].url == timeslip.project) {
to:
if ($scope.projectsObject[ii].url == timeslip.project.url) {
#1
0
I suggest to use a more concise code.
我建议使用更简洁的代码。
What has changed:
发生了什么变化:
- Iteration for
response.data.timeslips
. - Iteration for
$scope.projectsObject
. - Test if property
url
is equal in both objects. - Provide a default value
0
, ifb.hours
is not set. - Conversion of
timeslip.hours
fromString
toNumber
. - Add the values.
- Assign the new value to
b.hours
(and leave the rest of object b intact).
迭代的响应.data.timeslips。
$ scope.projectsObject的迭代。
测试两个对象中属性url是否相等。
如果未设置b.hours,则提供默认值0。
将timeslip.hours从String转换为Number。
添加值。
将新值分配给b.hours(并保持对象b的其余部分完整)。
var response = { data: { timeslips: [{ "hours": "7.0", "url": "api.sample.com" }, { "hours": "2.5", "url": "api.sample1.com" }, { "hours": "3.1", "url": "api.sample1.com" }, { "hours": "0.5", "url": "api.sample2.com" }] } },
$scope = { projectsObject: [{ "url": "api.sample1.com" }, { "url": "api.sample.com" }, { "url": "api.sample2.com" }] };
response.data.timeslips.forEach(function (timeslip) {
$scope.projectsObject.forEach(function (b) {
if (timeslip.url === b.url) {
b.hours = (b.hours || 0) + +timeslip.hours;
}
});
});
document.write('<pre>' + JSON.stringify($scope, 0, 4) + '</pre>');
#2
0
Change:
if ($scope.projectsObject[ii].url == timeslip.project) {
to:
if ($scope.projectsObject[ii].url == timeslip.project.url) {