Ad

How To Dynamically Generate Model Names In A Service

I have a service that i am using to apply a unique number to a model name. the result i am getting is this

 "sectionInputs": [
  {
    "model": "price_min1"
  },
  {
    "model": "price_max2"
  },
  {
    "model": "units_occ3"
  },
  {
    "model": "inv_mod4"
  },
  {
    "model": "inv_fin5"
  },
  {
    "model": "inv_vdl6"
  },
  {
    "model": "inv_uc7"
  },
  {
    "model": "inv_fut8"
  },
  {
    "model": "inv_con9"
  },
  {
    "model": "units_total10"
  }
]

i need this to each have '1'. and then in the next object array i need them to have '2', etc... as of now every object array looks like this. i have a plunker with everything setup. plunker

function sectionInputSvc(sectionInputs) {

var vm = this;

vm.sectionInputsArry = sectionInputs;

vm.sectionInputs = function () {
    var arry = [];
    var counter = 0;

    for (var i = 0; i < vm.sectionInputsArry.length; i++) {
        counter++
        var obj = {
            model: vm.sectionInputsArry[i].model + counter
        };
        arry.push(obj);
    };

    return arry;

};

};

Ad

Answer

[EDIT 2]

in app.js...

sections[i].sectionInputs = sectionInputSvc.sectionInputs(sections[i],i);

and in section.service.js...

    function sectionInputSvc(sectionInputs) {

    var vm = this;

    vm.sectionInputsArry = sectionInputs;
    var obj2={};

    vm.sectionInputs = function (obj2,num) {
        var arry = [];
        var counter = 0;

        for (var i = 0; i < vm.sectionInputsArry.length; i++) {
            counter++
            var obj = {
            model: vm.sectionInputsArry[i].model + num
        };
            arry.push(obj);
        };

        return arry;

    };
};
Ad
source: stackoverflow.com
Ad