javascript - adding properties to an existing object inside an array -
i picking array few object on external file.
using:
app.controller('mycontroller', function($scope, $http) { $http.get("file") .then(function(response) { $scope.lols = response.data; });});
this give me like:
$scope.lols = [ { prop1: "h1", prop2: "h2", }, { prop1: "g1", prop2: "g2", },}
now want add prop3 in each of objects how should ? if had data js file manually picking data external file...
i have tried 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 has not worked...
thanks or link explain it.
solution: https://jsfiddle.net/d3c96e0z/3/
you want prop3
sibling of prop1
, prop2
? if can either hard-coded like:
$scope.lols[0].prop3 = "h3"; $scope.lols[1].prop3 = "g3";
or in more dynamic way:
var newprops = ["h3", "g3"]; (var = 0; < $scope.lols.length; i++) { $scope.lols[i].prop3 = newprops[i]; // assuming length of both arrays same size; }
the problem approach: push
you're creating new index in array instead of adding properties existing indexes.
Comments
Post a Comment