0

I am working with a project that does not use jquery. I have been using underscore or vanilla javascript. Here is a plunker I have with what I got. plunker

// Code goes here
var successfullIdArray = ["713","281","555"];

var tmpTripsArry = [{
            "routeUUID": "123",
            "routeLocations": {
                "isSubmitLocationSuccess": false,
                "routeUUID": "123",
                "locationId": "713"
            }
        },{
            "routeUUID": "909",
            "routeLocations": {
                "isSubmitLocationSuccess": false,
                "routeUUID": "909",
                "locationId": "281"
            }
        },{
            "routeUUID": "800",
            "routeLocations": {
                "isSubmitLocationSuccess": false,
                "routeUUID": "800",
                "locationId": "555"
            }
        },{
            "routeUUID": "444",
            "routeLocations": {
                "isSubmitLocationSuccess": false,
                "routeUUID": "444",
                "locationId": "200"
            }
        }];    

 var tmpUpdatedTripsArry = _.each(tmpTripsArry, function () {
        _.find(tmpTripsArry, function (item) {
          if (successfullIdArray.locationId === item.routeLocations.locationId) {
               item.routeLocations.isSubmitLocationSuccess = true;
                   return item;
                } else {
                   return item;
                }
            });
        });        

        console.log(tmpUpdatedTripsArry)

I need to compare the tmpTripsArry nested property of locationId against the successfullIdArray and return the matching objects with the

isSubmitLocationSuccess

changed to true.

I am not getting any errors but it is not doing what I need it to do. thanks. prefer to use underscore

1 Answer 1

2

simply js

var matchingObjs = tmpTripsArry.filter( function(val){
  if ( successfullIdArray.indexOf( val.routeLocations.locationId ) != -1 )
  {
    val.routeLocations.isSubmitLocationSuccess = true;
  }
  return successfullIdArray.indexOf( val.routeLocations.locationId ) != -1 ;
}) 

If you need all the objects to be returned, then use forEach instead

tmpTripsArry.forEach( function(val){
  if ( successfullIdArray.indexOf( val.routeLocations.locationId ) != -1 )
  {
    val.routeLocations.isSubmitLocationSuccess = true;
  }
}) 

Now tmpTripsArry itself has the modified values.

Sign up to request clarification or add additional context in comments.

1 Comment

great! Also I forgot to mention I need all of the objects to be returned, even if there are no matching ids

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.