将一个数组中的值添加到具有指定键和索引的对象
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
的值
我需要的是,在我执行 push 之前,用以下值填充 listTypeGroup
& listTypeGroupDescription
(带有红色箭头)在 oData -> ListTypeGroupAssigment -> result
(listTypeGroup & listTypeGroupDescription) 中, index 是相关的,因为我只想在每次迭代中添加索引的值(因为这段代码是在外循环中调用的和索引确定循环的当前步骤),如何才能很好地完成?
结果包含 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()
函数 returns 到底是什么还不清楚。我假设它是已填充的 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);
}
});
这是应用了函数的 fiddle 中的数据:https://jsfiddle.net/hyz0s5fe/
选项二 - 仅当 属性 存在于 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;
}
}
}
Fiddle 对于选项 2:https://jsfiddle.net/4rh6tt25/
我正在使用以下代码,
jQuery.each(aDataSel, function(index, oData) {
oPushedObject = {};
aSelectedDataSet.push(fnCreateEnt(aProp, oData, oPushedObject));
});
这是一个SelectedDataSet值
这是 OData
的值我需要的是,在我执行 push 之前,用以下值填充 listTypeGroup
& listTypeGroupDescription
(带有红色箭头)在 oData -> ListTypeGroupAssigment -> result
(listTypeGroup & listTypeGroupDescription) 中, index 是相关的,因为我只想在每次迭代中添加索引的值(因为这段代码是在外循环中调用的和索引确定循环的当前步骤),如何才能很好地完成?
结果包含 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()
函数 returns 到底是什么还不清楚。我假设它是已填充的 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);
}
});
这是应用了函数的 fiddle 中的数据:https://jsfiddle.net/hyz0s5fe/
选项二 - 仅当 属性 存在于 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;
}
}
}
Fiddle 对于选项 2:https://jsfiddle.net/4rh6tt25/