0

I am making an app that requires the user to click on a location and then will be directed to the location via Google Maps.The locations are an array of objects. This is what the location files look like.

$scope.SiteLocs = [
          {
            "name": "502 Nelson St, Greenville, MS 38701",
            "visibility": "0",
            "description": "502 Nelson St, Greenville, MS 38701",
            "styleUrl": "#waypoint",
            "Point": { "coordinates": "-91.05636,33.415485,0" }
          },
          {
            "name": "242 Blackhawk Trace, Galena, IL 61036",
            "visibility": "0",
            "description": "242 Blackhawk Trace, Galena, IL 61036",
            "styleUrl": "#waypoint",
            "Point": { "coordinates": "-90.319778,42.390862,0" }
          },
          {
            "name": "3747 Ocean Dr, Vero Beach, FL 32963",
            "visibility": "0",
            "description": "3747 Ocean Dr, Vero Beach, FL 32963",
            "styleUrl": "#waypoint",
            "Point": { "coordinates": "-80.358248,27.659094,0" }
          }, ect...

I want to add another element to every item in the array. I tried adding it in through the .concat() method but that didn't seem to work. If I wanted to add "carrier": "sprint", to each location how would I do that?

I forgot to add this bit of code. It splits and reorders the coordinates in the files.

angular.forEach($scope.SiteLocs, function(location) {
      var clength = location.Point.coordinates.length;
      if (location.Point.coordinates.substring(clength - 2, clength) === ",0") {
        location.Point.coordinates = location.Point.coordinates.substring(0, clength - 2).split(",");
        Lat = location.Point.coordinates[0]
        Lon = location.Point.coordinates[1]
        Com = ","
        location.Point.coordinates = Lon.concat(Com,Lat)
      }
2
  • This is not a "JSON array"! This is a JavaScript array containing JavaScript objects. JSON is a textual data exchange format, just like XML, CSV or YAML. Commented Jul 16, 2014 at 18:08
  • Gotcha, Sorry for the mistake! Commented Jul 16, 2014 at 18:10

3 Answers 3

2

Have you looked at angular.forEach? https://docs.angularjs.org/api/ng/function/angular.forEach

Here's the working JSFiddle - http://jsfiddle.net/Wqu68/3/

Relevant code:

function appCtrl($scope, $http){

    $scope.SiteLocs = [
          {
            "name": "502 Nelson St, Greenville, MS 38701",
            "visibility": "0",
            "description": "502 Nelson St, Greenville, MS 38701",
            "styleUrl": "#waypoint",
            "Point": { "coordinates": "-91.05636,33.415485,0" }
          },
          {
            "name": "242 Blackhawk Trace, Galena, IL 61036",
            "visibility": "0",
            "description": "242 Blackhawk Trace, Galena, IL 61036",
            "styleUrl": "#waypoint",
            "Point": { "coordinates": "-90.319778,42.390862,0" }
          },
          {
            "name": "3747 Ocean Dr, Vero Beach, FL 32963",
            "visibility": "0",
            "description": "3747 Ocean Dr, Vero Beach, FL 32963",
            "styleUrl": "#waypoint",
            "Point": { "coordinates": "-80.358248,27.659094,0" }
          }]

      angular.forEach($scope.SiteLocs, function(place) {
          place.carrier = "Sprint";
     });

}

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

7 Comments

Yeah I have, I forgot to put that in my question. I added in the code in my question.
Are you trying to modify SiteLocs after it has already been filled with all the prior information? Also, did you have a question about the forEach that you just posted?
Yeah, Im trying to add "Carrier": "sprint" to each object in SiteLocs. Also, No, I dont have a question on the forEach, I just thought id add it in if it helps.
Haha, after looking at AngularJS's forEach code, I would say it's way too complicated for this addition. I'd just use aarosil's advice and use a simple for loop or non-angularjs forEach
Actually I take that back, haha sorry. It's actually pretty simple, I just derped. I posted a working JSFiddle
|
0

you can do it right there in the loop you already have angular.forEach($scope.SiteLocs, function(location) {...} just add location.carrier = "sprint"; in that function.

Comments

0

Sometimes I use lodash or angular. They all similar.

**Solution** using Lodash.js

//using forEach to loop each array
_.forEach($scope.SiteLocs, function(object){

***//adding carrier as property to each object in the array and giving value 'sprint'
// an identifier/variable can take the place of that value***

    object.carrier = 'sprint';
  });

**Solution** using angular.js
//using forEach to loop each array

angular.forEach($scope.SiteLocs, function(object){

***//adding carrier as property to each object in the array and giving value 'sprint'
// an identifier/variable can take the place of that value***

    object.carrier = 'sprint';
  });

Comments

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.