0

I am new to javascript so i dont know how to create object once we have values dynamically , so below code i have fullName and workerKey from dataItem now i want to create object selectedOwners with values of fullName and workerKey.

How can i achieve that task ?

ctrl.js

  var selectedOwners = {};
  $scope.addProcessOwner = function(dataItem){
    var fullName = dataItem.fullName;
    var workerKey = dataItem.workerKey;
    console.log('WORKER KEY', workerKey);
  }
hussain
  • 6,587
  • 18
  • 79
  • 152
  • may be help you http://stackoverflow.com/questions/32470928/angular-formly-adding-form-fields-dynamically-on-user-click/35603088#35603088 – Hadi J Mar 08 '16 at 16:31

3 Answers3

1

You use an object initializer:

selectedOwners = {
    fullName: dataItem.fullName,
    workerKey: dataItem.workerKey
};

The object initializer is the {...} bit. Each of those two things inside it is a property initializer. The part before the : is the name, the part after is the value, which can be the result of any expression.

In your code, you'd already created the object (var selectedItem = {};). The code above will replace that object. If you just wanted to add to it, you'd just use assignment:

selectedItem.fullName = dataItem.fullName;
selectedItem.workerKey = dataItem.workerKey;

Which you use depends on whether it matters that you not create a new object.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Edited, as per comments:

var list = [];   
$scope.addProcessOwner = function(dataItem){
       var selectedOwners = {"fullname":dataItem.fullName,"workerKey":dataItem.workerKey};
    list.push(selectedOwners);
    }

// use list to populate output 
Taran J
  • 805
  • 6
  • 10
  • For what it's worth, you don't have to quote the object keys. Ex: `{ fullName: dataItem.fullName, workerKey: dataItem.workerKey }`. – Mike Cluck Mar 08 '16 at 16:34
  • And `Object.assign` will copy *everything* from `dataItem`, not just the selected properties. – T.J. Crowder Mar 08 '16 at 16:36
  • Thanks for the answer is it good approach later i have to assign workeyKey to the form field `$scope.processDTO.processOwnerWorkerKey` and fullName to `$scope.processDTO.prcsOwner` ther will be multiple selections of owners – hussain Mar 08 '16 at 16:37
  • @Mike C: yes, kind off stuck with using quotes – Taran J Mar 08 '16 at 16:37
  • @TaranJ What do you mean you're "stuck with using quotes?" The language doesn't force you to. That's what I was saying. – Mike Cluck Mar 08 '16 at 16:38
  • @hussain: you can simply use it as selectedOwners["fullname"] or selectedOwners.fullname to assign them to the form field – Taran J Mar 08 '16 at 16:40
  • @Mike C: agreed. I mean I'm stuck with using it that way, for I use selectedOwners["fullname"] and selectedOwners[fullname] means something else. – Taran J Mar 08 '16 at 16:44
  • @TaranJ You're not making sense. You don't need to use brackets *or* quotes. Just do "dot name". Ex: `selectedOwners.fullName`. That's the exact same thing as `selectedOwners["fullName"]`. – Mike Cluck Mar 08 '16 at 16:45
  • @Mike: read my comments above for hussain. I know dot notation :). just that I prefer using quotes. – Taran J Mar 08 '16 at 16:49
  • 1
    Thanks all of you it helped alot and i got the values assigned to object properties. – hussain Mar 08 '16 at 16:49
  • 1
    @TaranJ Then don't say you're "stuck" with them. You're not stuck with them, you prefer them. – Mike Cluck Mar 08 '16 at 16:50
  • one issue i see its creating everytime new object lets say if user select 3 different owners i want to display all three on the form but in current case its only populating last one – hussain Mar 08 '16 at 16:53
  • @Mike: yes thanks, 'prefer' was the word I was looking for :p. Hussain: you can push all 'selectedOwners' into an array and display them all, editing this answer, check it out – Taran J Mar 08 '16 at 16:58
0

You have already created the object so all you need to do is add the values into it.

    var selectedOwners = {};
    $scope.addProcessOwner = function(dataItem){
        selectedOwners.fullName = dataItem.fullName;
        selectedOwners.workerKey = dataItem.workerKey;
        //This will print out the newly populated object
        console.log(selectedOwners);
    }
Joffutt4
  • 1,418
  • 14
  • 15
  • 1
    That code doesn't *add* values to the object the OP created. It creates a *new* object. – T.J. Crowder Mar 08 '16 at 16:40
  • Edited it to take into account what you stated. Realized you were correct and that it would re-create the object instead of actually adding the values to the already created one – Joffutt4 Mar 08 '16 at 16:44