0

I need to detect an object in an array with its Id.

My first array looks like that:

{ [id: 9, name: 'abc'], [id: 2, name 'def'], [id: 40, name: 'gh'] } (Id & name),

while that other array is:

{ [class: 'physics', Tid: 9], [class: 'computer science', Tid: 9], [class: 'Biology', Tid: 40] }.

I need to match the parameter "name" from the first array by its ID to its "class" (for example, "physics" relates to Tid=9 which is "abc" and "Biology" relates to Tid=40 which is "gh").

How can I elegantly do so without changing the way the data comes? (It comes from a database with ASP.NET web service in JSON)

GG.
  • 21,083
  • 14
  • 84
  • 130
Gil
  • 55
  • 7

2 Answers2

0

You could use $http.get() which has success and error callback functions, which returns a promise object. Using this, you can setup a condition to map the id and get your desired result.

Something like this.

var myObject1 = {};
var myArray1 = [];

var myObject2 = {};
var myArray2 = [];

$http.get('json-file')
        .success(function(data)) {
            myObject1.myArray1 = data;
        }

$http.get('json-file')
        .success(function(data)) {
            myObject2.myArray2 = data;
        }

/* inside a loop if required */
if (myObject1.myArray1[index].id == myObject2.myArray2[index].Tid) {
    /* perform logic */
}

This code would be present inside a service or a controller.

Haven't tested it so unsure of the syntax but promise objects are the way to go.

Hope this helps.

Prathap
  • 178
  • 1
  • 1
  • 9
  • 1
    actually, Angular has deprecated the .success/.error functions in favor of the .then(success, failure) pattern. http://stackoverflow.com/questions/35329384/why-is-angular-http-success-error-being-deprecated – hsotweelvl Dec 19 '16 at 20:58
  • Thanks for the insight. Will incorporate in my code too from now :) – Prathap Dec 19 '16 at 21:11
0

This returns an array of arrays. Each array in the array contains two objects matched by id === Tid. As far as I can tell that's what you want.

(Note that I think you provided broken sample arrays, I adjusted and scrambled the numbers around so you could see it work more clearly).

var arr1 = [ {id: 9, name: 'abc'}, {id: 2, name: 'def'}, {id: 40, name: 'gh'} ];
var arr2 = [ {class: 'physics', Tid: 2}, {class: 'computer science', Tid: 40}, {class: 'Biology', Tid: 9} ];

var arrFinal = arr1.map ( function ( d ) {
    var matched = arr2.find ( function ( obj ) {
       return obj.Tid === d.id;
    } );

    return [ d, matched ];
} );

If you iterate arrFinal you'll see it contains the matched objects.

Tim Consolazio
  • 4,802
  • 2
  • 19
  • 28
  • BTW if there isn't a match for a given object in arr1, then the matched array will contain [ objFromArr1, undefined ]. That's not broken, it's just telling you, "there was no match for this object". It also assumes the properties "id" and "Tid" will always be on those objects, if they're not it'll break, so guard with ( if [prop] in obj ) and so on. – Tim Consolazio Dec 19 '16 at 21:01