[英]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的值
我需要的是,我做的前推是填补listTypeGroup
& listTypeGroupDescription
(用红色箭头)的各种价值观,里面却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.