0

I am picking an array with few object on it from an external file.

Using:

app.controller('mycontroller',  function($scope, $http) { 
$http.get("file")
.then(function(response) {
    $scope.lols = response.data;     
});});

This will give me back something like:

 $scope.lols = [
{
    prop1: "h1",
    prop2: "h2",
},
{
    prop1: "g1",
    prop2: "g2",
},}

Now I want to add a prop3 in each of the objects how should I do it ? If I had the data into my js file I will do it manually but picking the data from an external file...

I have tried simply doing:

app.controller('mycontroller',  function($scope, $http) { 
$http.get("file")
.then(function(response) {
    $scope.lols = response.data;
    $scope.lols.push = [
    {prop3: "h3"},
    {prop3: "g3"}
    ]
});});

But it has not worked...

Thanks for any help or link explain it.

SOLUTION: https://jsfiddle.net/d3c96e0z/3/

3
  • 1
    What is $scope.games.push = [? Commented Sep 22, 2016 at 12:17
  • can u be more clear on to which array has to modified ? . games array or lols arrays . :) Commented Sep 22, 2016 at 12:17
  • Please find here what I am trying to achieve jsfiddle.net/d3c96e0z Commented Sep 22, 2016 at 13:25

5 Answers 5

1

I think this should help you:-

var lols = [
{
    prop1: "h1",
    prop2: "h2",
},
{
    prop1: "g1",
    prop2: "g2",
}
]

Object.keys(lols).map(function(key,index){
        var lol = lols[key]
      lol["props3"]="g3"
      console.log(lol)
})

JS fiddle link https://jsfiddle.net/bps7zzf8/

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

1 Comment

Please find here what I am trying to achieve jsfiddle.net/d3c96e0z
0

You want prop3 to be a sibling of prop1 and prop2? If so you can do it either hard-coded like:

$scope.lols[0].prop3 = "h3";
$scope.lols[1].prop3 = "g3";

Or in a more dynamic way:

var newProps = ["h3", "g3"];
for (var i = 0; i < $scope.lols.length; i++) {
  $scope.lols[i].prop3 = newProps[i]; // assuming the length of both arrays is the same size;
}

The problem with your approach: With push you're creating a new index in the array instead of adding your properties to existing indexes.

8 Comments

each object has a diferent prop3 value
and from where do you get those values? Then I can tell you how to add them dynamically. What's the data structure of the source?
I updated my answer with a dynamic solution assuming your source is an array with the same length.
Michael , thanks for your help, this is a more clear example of what I am trying to achieve link
that means you know the length of the array you receive and you can create a new array with the same length, right? So you can use the dynamic example above just change the content of newProps like: var newProps = ["megaman", "jesus!"]; and put the code after $scope.lols = response.data; of course.
|
0

try like, Here is working example $scope.lols.push({prop1: "h3",prop2: "g3"});

1 Comment

With that you add a new object to the array but i want to keep the same object I have extracted from the external files and just add 1 more property yo each one
0

Keep the properties to be added in a array of objects and then loop over the $scope.lols and add each property according to the index;

$scope.propertyJson=[{prop3: "h3"},{prop3: "g3"}];
for (var i = 0; i < $scope.lols.length; i++) {
 angular.forEach($scope.propertyJson[i], function(value, key) {
  $scope.lols[i][key] = value;
 });
}

Comments

0

if you want to add prop3 in each object can try this process:

// assume that you know "lols" length. here let 2 so add 2 values for prop3

$scope.prop3Values = ["yellow", "red"];

$scope.lols = $scope.lols.map(function(e,index) {
    e.prop3 = $scope.prop3Values[index]; // assign your value in prop3
    return e;
  });

then your lols will be look like

$scope.lols = [
{
    prop1: "h1",
    prop2: "h2",
    prop3: "yellow"
},
{
    prop1: "g1",
    prop2: "g2",
    prop3: "red"
}]

3 Comments

If the value of the prop3 need to be for example "yellow" in the first object and "red" in the next object how would it be?
Please find here what I am trying to achieve jsfiddle.net/d3c96e0z
if you know length of lols array and what value need to add for prop3 then add all values in an array and assign it by using index. see my update answer @noway

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.