简体   繁体   中英

Transferring Properties from one to another object based on the value of another property

I have a slight problem. I am working on an angular application and at some point i am getting some data from an 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.

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.

The project array is : $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.

It would be great if someone could point out my mistake here, as I just can't seem to find it.

Thanks a lot!

Change:

if ($scope.projectsObject[ii].url == timeslip.project) {

to:

if ($scope.projectsObject[ii].url == timeslip.project.url) {

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 , if b.hours is not set.
  • Conversion of timeslip.hours from String to Number .
  • Add the values.
  • Assign the new value to b.hours (and leave the rest of object b intact).

 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>'); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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