1

I have following arrays with values (i am generating the values on go)

$scope.objectName = [{ Name: '' }];
$scope.propertiesElement = [{ Key: '', Value: '' }];

I want to concatenate these two objects to get the following result

[{Name:''},{ Key: '', Value: '' }]

Plunker link , somehow that's not working either

when I click on the add row button it will add another row for key and value text boxes only not for name, I can add n no of rows and when I click on Submit it should show the kev value pair as

[{Name:''},{ Key: '', Value: '' },{ Key: '', Value: '' },{ Key: '', Value: '' }.....so on]

Thanks

1
  • 1
    Uh.... why do you want the name in the same array as your key value pairs. Couldn't you submit it like this: var submission = {}; submission.name = $scope.objecName.name; submission.keyValuePairs = $scope.propertiesElement; Commented May 22, 2014 at 19:29

1 Answer 1

2

Not sure why you want to build an array of mismatched objects. That seems to me to be asking for trouble. I would suggest possibly doing the following:

$scope.objects = [{Name: '', Elements: []}];

Then you can easily manage multiple objects who have elements:

(I use underscore http://underscorejs.org/)

$scope.addElementToObject = function(objName, element){
   _.where($scope.mergedArray, {Name: objName}).Elements.push(element);
};

Then you can add to the list of elements for that object without having to eval the object in the elements array on each use.


If you still want/need to merge arrays of mismatched objects, it would be the following:

$scope.objectName = [{ Name: '' }];
$scope.propertiesElement = [{ Key: '', Value: '' }];
$scope.mergedArray = $scope.objectName.contact($scope.propertiesElement);

$scope.addElement = function(element){
   $scope.mergedArray.push(element);
};

Then, in your click event code:

$scope.addElement({ Key: 'someKey', Value: 'Some Value' });

I hope this helps.

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

2 Comments

Still my early days with AngularJs, so have to post questions like this to get acquainted with it .. I did the change that Mike suggested, and it worked Thanks anyways :)
Actually, this wasn't really an Angular question. It's more of a pure javascript question. There's nothing strictly Angularly about my solution (or Mike's). It's all just knowing how ECMA 5 Script (javascript) works. en.wikipedia.org/wiki/ECMAScript

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.