How to combine two JSON Objects in Angular

11,299 views
Skip to first unread message

Ren Vile

unread,
Oct 23, 2014, 9:19:30 AM10/23/14
to ang...@googlegroups.com
I would like to combine two JSON objects in Angular but I don't know enough about angular to understand what I'm doing wrong.  I've searched for hours already and can't seem to progress any further.  Please see code and pictures below to see what my results are.

  • Essentially I'm trying to concatenate two different models into one.
  • On the first pass it adds 3 models from JSON.
  • On the second pass it will add one more model (XL-Z232) but this is where it doesn't function correctly.

But when I combine them the joined array shows as and Object array with a Standard Array.  Please take a look!

   
 var oldtags;
   
var newtags;

    $scope
.getModels = function () {

       
if ($scope.tags.length > 0) {

            $http
.post('../../../ProductWS.asmx/FetchModelsList', { 'make': 'MYMAKE', 'category': 'audio', 'subcategory': 'cd player', 'year': '1999' })
           
.success(function (data, status, headers, config) {
               
                newtags
= data.d;                
           
}).
            error
(function (data, status, headers, config) {
               
// log error
           
}).then(function (results) {    
                     
               
var newlist = oldtags.concat(newtags);
             
                $scope
.tags = newlist;
                console
.log(newlist);


               
//});
               
});


       
}
       
else {
            $http
.post('../../../ProductWS.asmx/FetchModelsList', { 'make': 'MyMake', 'category': 'video', 'subcategory': 'Blu Ray Player', 'year': '2012' })
           
.success(function (data, status, headers, config) {
                $scope
.tags = data.d;
                oldtags
= $scope.tags;
                console
.log(oldtags);

           
}).
            error
(function (data, status, headers, config) {
               
// log error
           
});
       
}

   
};




Adrian Lynch

unread,
Oct 23, 2014, 9:39:17 AM10/23/14
to ang...@googlegroups.com
Array.concat joins two or more arrays. It's not a problem with Angular, just straight-up JS.

So regarding:

var newlist = oldtags.concat(newtags);

If oldtags is an array of strings and newtags is an array of objects, you can't just concat them if you want them to be the same.

var newlist = oldtags.concat(newtags.map(function(item) { return item.text; }));

Untested but give it a try!

Adrian


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Adrian Lynch

unread,
Oct 23, 2014, 9:55:01 AM10/23/14
to ang...@googlegroups.com
Console log new tags and old tags and post 'em here.

A

On 23 October 2014 14:51, Ren Vile <rene...@gmail.com> wrote:
Getting:
TypeError: a is undefined...

Ren Vile

unread,
Oct 23, 2014, 10:08:51 AM10/23/14
to ang...@googlegroups.com
Old
[Object { text="SR-HD1250"}, Object { text="SR-HD1500"}, Object { text="SR-HD2500US"}]


New
["XL-Z232"]

Ren Vile

unread,
Oct 23, 2014, 10:18:23 AM10/23/14
to ang...@googlegroups.com
Also please take a look at the js on this link.

plnkr with code

Thanks for your help!!!!

Adrian Lynch

unread,
Oct 23, 2014, 10:48:16 AM10/23/14
to ang...@googlegroups.com
var oldTags = [{text: 'xyz'}, {text: 'abc'}];
var newTags = ['New!'];
var combinedTags = oldTags.concat(newTags.map(function(tag) { return {text: tag} }));

console.log(oldTags);
console.log(newTags);
console.log(combinedTags);

Array.map is modern browser only. So you'll need to use jQuery's or underscore/lodash's version.

Adrian

Adrian Lynch

unread,
Oct 23, 2014, 11:01:11 AM10/23/14
to ang...@googlegroups.com
Try this:

var newlist = $scope.oldtags.concat($scope.newtags.map(function (tag) { return {text: tag} }));

In place of line 20 or 22.

Ren Vile

unread,
Oct 23, 2014, 11:11:19 AM10/23/14
to ang...@googlegroups.com

Ok awesome this is working.  Thanks!!!  :)

Reply all
Reply to author
Forward
0 new messages