2

I have two arrays, which are almost identical in structure, except for one has a property:value pair on every object, and the other doesn't.

Newcastle Array (with clubCode property)

 [
            {"clubCode": "newcastle-united",
                "firstName": "Tim",
                "lastName": "Krul",
                "position": "gk"},

            {"clubCode": "newcastle-united",
                "firstName": "Rob",
                "lastName": "Elliot",
                "position": "gk"}
]

Arsenal Array (without clubCode property)

 [
            {"firstName": "Petr",
                "lastName": "Cech",
                "position": "gk",

            {"firstName": "David",
                "lastName": "Ospina",
                "position": "gk"}
] 

Is it possible to push() the clubCode property to every object within the Arsenal array, so I don't have to manually add it to every single one?

...so each object starts with "clubCode": "arsenal".

Or if there is any other solution I'd love to hear it.

Any help will be appreciated. Thanks in advance.

2
  • on which basis you want to add it and how is your final data structure look? Commented Mar 3, 2016 at 18:36
  • I'm running AngularJS, and I'm accessing the $scope.arsenalSquad array by calling a JSON file, which is that same array. The array is returned as arsenalSquad.data. In my controller, I want to write the necessary code to return every object within that array with the property of clubCode and the corresponding value of arsenal. Commented Mar 3, 2016 at 19:34

4 Answers 4

4

You can use Array.prototype.forEach over your data structure.

var x =  [
            {"firstName": "Petr",
                "lastName": "Cech",
                "position": "gk",
            },
            {"firstName": "David",
                "lastName": "Ospina",
                "position": "gk"}
         ]; 

x.forEach(function(e){e.clubCode='arsenal';})
Sign up to request clarification or add additional context in comments.

Comments

1

Try to use Array.prototype.map() at this context,

arsenalArray = arsenalArray.map(function(itm){
  return (itm.clubCode = "arsenal", itm);
});

DEMO

1 Comment

I tried this, but it didn't work. My problem is, I'm pulling the arsenalSquad array from a JSON file and the Service I'm using is returning it as arsenalSquad.data. So my final code looked like this: $scope.arsenalSquad.data = $scope.arsenalSquad.data.map(function(itm){ return (itm.clubCode = "arsenal", itm); }); Can you see what's wrong here?
0
    $scope.ar1 = [
            {"clubCode": "newcastle-united",
                "firstName": "Tim",
                "lastName": "Krul",
                "position": "gk"},

            {"clubCode": "newcastle-united",
                "firstName": "Rob",
                "lastName": "Elliot",
                "position": "gk"}
];

    $scope.ar1.forEach(function (value) {
      value.clubCode = "newcastle-united";
    });

    console.log($scope.ar1);

Comments

0

Its very easy using lodash.js

_.assign(arsenalArray, { clubCode: "arsenal" });

1 Comment

That does look easy. Would that work in AngularJS? My issue is I'm using a Service to pull the arsenalSquad array from a JSON file, and it's returning the array as arsenalSquad.data. So, in your case, would I write: _.assign(arsenalSquad.data, {clubCode: "arsenal" });?

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.