0

First of all: I already found this thread, which basically is exactly what I want, but I tried my best to apply it to my needs - I couldn't.

So, I have the following javascript function:

function loadRelationData(object) {
    var result = [];
    var parents = []
    parents = getParentObjectsByObjectID(object['ObjectID']);

    var tmpFirstObjects = [];
    var tmpOtherObjects = [];
    $.each(parents, function (_, parent) {
        var keyName = 'Übergeordnete ' + parent['ObjectType'];
        var pushObject = {};
        if (parent['ObjectType'] == object['ObjectType']) {
            pushObject['Fieldname'] = keyName;
            pushObject['Value'] = parent['Name'];
            tmpFirstObjects.push(pushObject);
        } else {
            pushObject['Fieldname'] = keyName;
            pushObject['Value'] = parent['Name'];
            tmpOtherObjects.push(pushObject);
        }
    });
    result = result.concat(tmpFirstObjects).concat(tmpOtherObjects);

    return result;
}

The parents array looks like this

And my function creates this result

This might be a bit complicated, but I need to split it up like this, because I need the order.

What I want is an array with both "TEC_MapLocations" joined together like this:

[
 {Fieldname: 'Übergeordnete TEC_Equipment', Value: 'E0192'}, 
 {Fieldname: 'Übergeordnete TEC_MapLocation', Value: ['M100', 'M200']}, 
 {Fieldname: 'Übergeordnete TEC_FunctionalLocation', Value: 'FL456'}
]

Any ideas on how to alter my code to achieve the desired result right away or how to merge the results array?

edit: I used Joseph's solution and used the following (quick and dirty) sort function to get back my desired sorting:

output.sort(function (a, b) {
    if (a.ObjectType == object.ObjectType) {
        return -1
    } else {
        return 1
    }
});
Community
  • 1
  • 1
Decay42
  • 802
  • 1
  • 9
  • 20

2 Answers2

0

Try this:

function joinObjects( array ) {

    // Start with empty array
    var ret = new Array();

    // Iterate array
    for ( var i = 0; i < array.length; i++ ) {

        // Search by fieldname
        var match = false;
        var j;
        for ( j = 0; j < ret.length; j++ ) {
            if ( array[i].Fieldname == ret[j].Fieldname ) { match = true; break; }
        }

        // If not exists
        if ( !match ) {

            // Intert object
            ret.push({
                Fieldname: array[i].Fieldname,
                Value: new Array()
            });

        }

        // Insert value
        ret[j].Value.push( array[i].Value );

    }

    // Return new array
    return ret;

}

http://jsfiddle.net/6entfv4x/

0

What you'd want to do first is build a hash with Fieldname as key, and an array as value. Then you'd want to use reduce to add the values into the hash and array. Then you can transform it into an array using Object.keys and map.

var input = [
  {Name: 'M100', ObjectID: 1, ObjectType: 'TEC_MapLocation'},
  {Name: 'M200', ObjectID: 2, ObjectType: 'TEC_MapLocation'},
  {Name: 'FL456', ObjectID: 4, ObjectType: 'TEC_FunctionalLocation'},
  {Name: 'E0192', ObjectID: 5, ObjectType: 'TEC_Equipment'}
];

var hash = input.reduce(function(carry, item){
  
  // Create the name
  var name = 'Übergeordnete ' + item.ObjectType;

  // If array with name doesn't exist, create it
  if(!carry[name]) carry[name] = [];

  // If item isn't in the array, add it.
  if(!~carry[name].indexOf(item.Name)) carry[name].push(item.Name);

  return carry;
}, {});

// Convert the hash into an array
var output = Object.keys(hash).map(function(key, index, array){
  return { Fieldname: key, Value: hash[key] }
});

document.write(JSON.stringify(output));
Joseph
  • 117,725
  • 30
  • 181
  • 234
  • Thank you, it works very well! But there is one thing missing: my function keeps the "TEC_Equipment" object in front, because that's where I need it. Your solution just sorts the results by ID. – Decay42 Sep 09 '15 at 14:27