1

Im using the following code,

jQuery.each(aDataSel, function(index, oData) {
        oPushedObject = {};
        aSelectedDataSet.push(fnCreateEnt(aProp, oData, oPushedObject));
    });

This is aSelectedDataSet values

enter image description here

and this is the values of OData

enter image description here

What I need is that before I do the push is to fill the listTypeGroup & listTypeGroupDescription (with the red arrow ) with values that Are inside the oData -> ListTypeGroupAssigment -> result (listTypeGroup & listTypeGroupDescription) , The index is relevant since I want to add just the value of the index in each iteration (since this code is called inside outer loop and the index determine the current step of the loop) ,How it can be done nicely?

The result contain 100 entries (always) and the a selected data will have 100 entries at the end...

Update :)

Just to be clear In the pic I show the values which is hardcoded for this run but the values can be any values, we just need to find the match between the both objects values...

I mean to find a match between to_ListTypeGroupAssigment in both object (which in this case exist ) and if in oData there is result bigger then one entry start with the matching ...

UPDATE2 - when I try Dave code the following happen for each entry, This happen in the Jquery.extend line...any idea how to overcome this?

enter image description here

The following hard-coded of Dave:-) work perfect but I need generic code which doesnt refer to specific field name

        jQuery.each(aDataSet, function(index, oData) {
            oPushedObject = {};
            fnCreatePushedEntry(aProperties, oData, oPushedObject);
            var result = oData.to_ListTypeGroupAssignment.results[index];
            oPushedObject.to_ListTypeGroupAssignment = {
                ListTypeGroup: result.ListTypeGroup,
                ListTypeGroupDescription: result.ListTypeGroupDescription
            };

            aSelectedDataSet.push(oPushedObject);
        });

Im stuck :(any idea how to proceed here ?what can be wrong with the extend ? should I use something else ? Im new to jQuery...:)

I think that this happen(in Dave answer) because the oData[key] is contain the results and not the specified key (the keyValue = to_ListTypeGroupAssignment ) which is correct but we need the value inside the object result per index...

enter image description here

var needValuesForMatch = {
    ListTypeGroup: 'undefined',
    ListTypeGroupDescription: 'undefined',
  }
  //Just to show that oPushedObject can contain additional values just for simulation 
var temp = {
  test: 1
};

//------------------This object to_ListTypeGroupAssigment should be filled (in generic way :) ------
var oPushedObject = {
  temp: temp,
  to_ListTypeGroupAssignment: needValuesForMatch
};

oPushedObject is one instance in aSelectedDataSet

and after the matching I need to do the follwing:

aSelectedDataSet.push(oPushedObject);
07_05_GuyT
  • 2,787
  • 14
  • 43
  • 88
  • if `fnCreateEnt` should create the entry (looks like, sounds like ;) you should add the index position of the results array as parameter to it. e.g. `aSelectedDataSet.push(fnCreateEnt(aProp, oData, oPushObj, index)` – hr_117 Mar 16 '16 at 15:29
  • Sorry but no. This is a moving target. By they way, to change the question without keep the original part in not helpful. – hr_117 Mar 17 '16 at 08:24
  • @hr_117 - Hi, what do you mean but moving target ? and to change the question ? I just updated it with helpful data I think.. if not let me know and I will handle it asap! thanks – 07_05_GuyT Mar 17 '16 at 08:27
  • Sorry again, my fault, I overlooked that the original code fragment ist still there. But this is still a moving target because a generic solution was not mentioned in the original request (if I'm right). Perhaps you should accept the initial answer form @dave and create a new question. With this it could be helpful if you show also what `fnCreateEnt` do and should do. – hr_117 Mar 17 '16 at 11:09
  • It is possible to use `$.extend`, but this will copy all properties and values from the relavant object in `oData` to the corresponding object in `aSelectedDataSet`, not just those which are `undefined`. Is that ok? – dewd Mar 17 '16 at 15:09
  • @shopiaT Pls review the answer I have added below. If it's not what you're after then fine, but some feedback would be nice. – dewd Mar 17 '16 at 16:07

2 Answers2

1

If I am understanding you correctly this should just be a small change:

jQuery.each(aDataSel, function(index, oData) {
  oPushedObject = {};
  fnCreateEnt(aProp, oData, oPushObj);

  //get all the properties of oData and clone into matching properties of oPushObj
  Object.getOwnPropertyNames(oData).forEach(function(key) {
    if (oPushObj.hasOwnProperty(key)) {
      //oPushObj has a matching property, start creating destination object
      oPushObj[key] = {};
      var source = oData[key];
      var destination = oPushObj[key];

      //can safely assume we are copying an object. iterate through source properties
      Object.getOwnPropertyNames(source).forEach(function(sourceKey) {
        var sourceItem = source[sourceKey];

        //handle property differently for arrays
        if (Array.isArray(sourceItem)) {
          //just copy the array item from the appropriate index
          destination[sourceKey] = sourceItem.slice(index, index + 1);
        } else {
          //use jQuery to make a full clone of sourceItem
          destination[sourceKey] = $.extend(true, {}, sourceItem);
        }

      });
    }
  });

  aSelectedDataSet.push(oPushedObject);
});

It is unclear what exactly your fnCreateEnt() function returns though. I am assuming it is the populated oPushObj but it's not entirely clear from your question.

Dave
  • 10,748
  • 3
  • 43
  • 54
  • Thanks but this is a bit more complicated the problem its hardcoded the values like ListTypeGroupAssignment and the value inside , what I need it generic solution that find the match for values without specific name just to see if both object have the same names... – 07_05_GuyT Mar 16 '16 at 15:43
  • Oh, you didn't say that in your question. I updated the answer so that all properties of `oData` are enumerated and the ones that exist in `oPushObj` are populated with clones of the items from `oData`. – Dave Mar 16 '16 at 15:58
  • HI Dave, I saw that the hardcoded values code is still there ,why :) ? 2. before the code worked as expected for one to one ,how its better to do that it will work the same as before (the code in the question if results array are greater the 1...Thank you very much!!! – 07_05_GuyT Mar 16 '16 at 18:46
  • I just forgot to clear that part when I updated the answer, sorry. Does it make sense now? – Dave Mar 16 '16 at 18:53
  • yes thanks now Im checking it, can you please also see my second question ,thank a lot! – 07_05_GuyT Mar 16 '16 at 19:00
  • now I see that the code is not working as expected :( I debug it to understand why, the typed code worked great! but now it seems that the object are changing ... – 07_05_GuyT Mar 16 '16 at 19:03
  • I think that something wrong with the extend ,any idea what can go wrong here ? +1 for helping me with this question thanks again – 07_05_GuyT Mar 16 '16 at 19:11
  • The problem here that if the condition is true(the if is enters ) inaSelectedDataSet the to_listTypeGroupAssigment values are overridden with all the 100 values ,I will add a screen shot in a min to my question. – 07_05_GuyT Mar 16 '16 at 19:27
  • The problem is that this is a lot of data :( your typed code work perfect!!! any idea why the extend is override the two values ListTypeGroup & ListTypeGroupDescription with the 100 entries? – 07_05_GuyT Mar 16 '16 at 19:41
  • Thank you but now it doesnt enter to the if just to the else and same results :( any other idea? – 07_05_GuyT Mar 16 '16 at 20:19
  • I'll try tomorrow, its too late here and this is lot of work :) , just 2 last question , the oPushedObject is identical in the code I put the oPushObj by mistake :( 2, how come that the harded-coded code of yours works perfect and now with the extend it doesnt any hint ? thank you very much Dave! – 07_05_GuyT Mar 16 '16 at 20:36
  • Ah, I think I see now. Looks like we need an additional loop. See the updated answer. – Dave Mar 17 '16 at 15:33
  • Thanks I will check it soon but see this jsFiddle...https://jsfiddle.net/m6zg8gcw/15/ – 07_05_GuyT Mar 17 '16 at 15:43
  • Thanks I check it and it doesnt work :( can you please have a look at the fiddle which i add in the comment – 07_05_GuyT Mar 17 '16 at 15:51
  • the fiddle reflect the objects in the jQuery extend line of your previews answer – 07_05_GuyT Mar 17 '16 at 16:04
1

Is this what you're after:

OPTION ONE - DEEP CLONE FROM oData TO aSelectedDataSet

aSelectedDataSet.forEach(function(currentObject,index){

     for (var childObject in currentObject) {
         if (! currentObject.hasOwnProperty(childObject))
             continue;

          var objectToClone = oData[childObject]['results'][index];

          if(objectToClone)
              $.extend(true,currentObject[childObject],objectToClone);
     }
  });

Here is your data in a fiddle with the function applied: https://jsfiddle.net/hyz0s5fe/

OPTION TWO - DEEP CLONE FROM oData ONLY WHERE PROPERTY EXISTS IN aSelectedDataSet

    aSelectedDataSet.forEach(function(currentObject,index){

     for (var childObject in currentObject) {
          if (! currentObject.hasOwnProperty(childObject))
            continue;

          if(typeof currentObject[childObject] !== 'object')
            continue;

          for(var grandChildObject in currentObject[childObject]) {

               var objectToClone = oData[childObject]['results'][index][grandChildObject];

                if(typeof objectToClone === 'object') {
                        $.extend(true,currentObject[childObject][grandChildObject],objectToClone);
                } else {
                        currentObject[childObject][grandChildObject] = objectToClone;
                }
          }
     }

Fiddle for option 2: https://jsfiddle.net/4rh6tt25/

dewd
  • 4,380
  • 3
  • 29
  • 43
  • it seems that almost :) can you please add the oPushedObject to your answer and answer it with the context of my question since im bit confused :(but its seems to work in fiddle then i'll check it on my real program and let you know :) thank you very much 1+ – 07_05_GuyT Mar 17 '16 at 16:17
  • the objects are merged into `aSelectedDataSet`, so no push is required. Or are you after not affecting `aSelectedDataSet` and creating a new object called `oPushedObject`? – dewd Mar 17 '16 at 16:27
  • Sorry for the delay in my response...can you just use this jsfiddle.net/m6zg8gcw/15 I need the oData object and the oPushedObject with the data you have created in your fiddle (thanks for that :) – 07_05_GuyT Mar 17 '16 at 19:08
  • Sorry, I am confused. Are you trying to merge `oData` to `aSelectedDataSet`, or make a copy called `oPushedObject` which is a copy of `aSelectedDataSet` merged with `oData` or something else altogether? I've looked at your fiddle and it's not obvious to me what you're trying to do. – dewd Mar 17 '16 at 19:30
  • oPushedObject reflect each of the instance that should be added to aSelectedDataSet which is at the end should have 100 entries with the key and the values .is it more clear now? – 07_05_GuyT Mar 17 '16 at 19:51
  • Maybe. So `oPushedObject` just needs to have the properties from `aSelectedDataSet ` which are `undefined` and the now in `oPushedObject` `undefined` items need to be populated with the corresponding items from `oData`? – dewd Mar 17 '16 at 22:49
  • yes oPushedObject contain the properties(with undfiend here its ListTypeGroup & listTypeGroupDescription) which should be added after the match,oPushed object is one instance which should be added to aSelectedDataSet,please see the update in my question with this info :) – 07_05_GuyT Mar 18 '16 at 06:54
  • @shopiaT Try this: jsfiddle.net/4rh6tt25/2 : It seems to meet these 2 criteria you specified "oPushedObject contain the properties(with undfiend" and "oPushed object is one instance which should be added to aSelectedDataSet" – dewd Mar 19 '16 at 00:10
  • Thanks for the help! Btw I have addional question with bounty that a bit similar to this ,please see if you can help here too,Thanks!http://stackoverflow.com/questions/36062777/how-to-match-values-between-object – 07_05_GuyT Mar 20 '16 at 20:05
  • @shopiaT I'll take a look at your other question. – dewd Mar 21 '16 at 14:42
  • @shopiaT Fiddle for your other question: https://jsfiddle.net/yh39of1b/3/ @ http://stackoverflow.com/questions/36062777/how-to-match-values-between-object | answer : http://stackoverflow.com/a/36136159/2298108 – dewd Mar 22 '16 at 00:02