簡體   English   中英

使用指定的鍵和索引將一個數組中的值添加到對象

[英]Add values from one array to object with specified key & index

我正在使用以下代碼,

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

這是一個SelectedDataSet值

在此處輸入圖片說明

這是OData的值

在此處輸入圖片說明

我需要的是,我做的前是填補listTypeGrouplistTypeGroupDescription (用紅色箭頭)的各種價值觀,里面卻oData -> ListTypeGroupAssigment -> result (listTypeGroup&listTypeGroupDescription),該指數是相關的,因為我想在每次迭代中僅添加索引的值(因為此代碼在外部循環內部被調用,並且索引確定循環的當前步驟),如何才能很好地完成?

結果包含100個條目(始終),並且所選數據的末尾將包含100個條目...

更新:)

只是為了清楚起見,在圖片中,我顯示了為此運行進行硬編碼的值,但是這些值可以是任何值,我們只需要找到兩個對象值之間的匹配即可...

我的意思是在兩個對象中找到to_ListTypeGroupAssigment之間的匹配項(在這種情況下存在),如果在oData中存在較大的結果,則從匹配項開始一個條目...

UPDATE2-當我嘗試使用Dave代碼時,每個條目都會發生以下情況,這發生在Jquery.extend行中...您知道如何克服這一點嗎?

在此處輸入圖片說明

以下Dave 硬編碼 :-) 可以完美工作,但我需要引用特定字段名稱的通用代碼

        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);
        });

我被困住了:(任何想法如何在這里進行? extend可能出什么問題?我應該使用其他東西嗎?我是jQuery的新手... :)

我認為發生這種情況(在Dave答案中)是因為oData [key]是包含結果而不是指定的鍵(keyValue = to_ListTypeGroupAssignment),這是正確的,但是我們需要每個索引的對象結果內的值...

在此處輸入圖片說明

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是aSelectedDataSet中的一個實例

匹配之后,我需要進行以下操作:

aSelectedDataSet.push(oPushedObject);

如果我正確地理解了您的意見,那么這應該只是一個小更改:

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);
});

目前還不清楚fnCreateEnt()函數到底返回什么。 我假設它是填充的oPushObj但是從您的問題中尚不完全清楚。

這是您所追求的嗎?

選項一-從oData到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);
     }
  });

這是應用了功能的小提琴中的數據: https : //jsfiddle.net/hyz0s5fe/

選項2-僅在aSelectedDataSet中存在屬性的oData中進行深度克隆

    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;
                }
          }
     }

選擇2的小提琴: https : //jsfiddle.net/4rh6tt25/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM