【发布时间】:2016-03-17 14:00:07
【问题描述】:
我有对象数组 oDataSet 和 aProperties 对象,我想匹配在 aPropertis 和 oDataSet 中找到的 相同 值并创建 aSelectedDataSet 开始是一个空对象
如何在 JS/Jquery 中推荐
注意:循环/解决方案中不应有任何硬编码 属性来进行匹配 aProperties 包含此值,但可以更改(当然,oData 对象中应该有匹配项......)
为了澄清以下是如何构建对象的示例
http://jsfiddle.net/4rh6tt25/5/
这是输入
//This is given array of object which can be many ,here I put just two instance in the array for demonstration purpose
var oDataSet = [{
__metadata: {
aaa: 111,
bbb: 222
},
to_ExcludedTerms: {results: []},
to_ListTypeGroupAssignment: {
results: [
{
AuthorisationGroup: 'AuthorisationGroup 1',
ListTypeGroup: 'ListTypeGroup1',
ListTypeGroupDescription: 'ListTypeGroupDescription 1',
ParentKey: '8ae25d47-c3cc-4ee3-a040-ea00505692111',
__metadata: {}
},
{
AuthorisationGroup: 'AuthorisationGroup 2',
ListTypeGroup: 'ListTypeGroup2',
ListTypeGroupDescription: 'ListTypeGroupDescription 2',
ParentKey: '34bcdc74-ab42-4538-8657-0a2b0473fcb7',
__metadata: {}
},
{
AuthorisationGroup: 'AuthorisationGroup 3',
ListTypeGroup: 'ListTypeGroup3',
ListTypeGroupDescription: 'ListTypeGroupDescription 3',
ParentKey: '34bcdc74-ab42-4538-8657-0a2b0473fcb5',
__metadata: {}
}
]
}
}, {
//This is the second instance of the object with same keys but different values
__metadata: {
aaa: 333,
bbb: 444
},
to_ExcludedTerms: {results: []},
to_ListTypeGroupAssignment: {
results: [
{
AuthorisationGroup: 'AuthorisationGroup 6',
ListTypeGroup: 'ListTypeGroup6',
ListTypeGroupDescription: 'ListTypeGroupDescription 6',
ParentKey: '8ae25d47-c3cc-4ee3-a040-ea00505692116',
__metadata: {}
},
{
AuthorisationGroup: 'AuthorisationGroup 7',
ListTypeGroup: 'ListTypeGroup7',
ListTypeGroupDescription: 'ListTypeGroupDescription 7',
ParentKey: '34bcdc74-ab42-4538-8657-0a2b0473fcb7',
__metadata: {}
},
{
AuthorisationGroup: 'AuthorisationGroup 8',
ListTypeGroup: 'ListTypeGroup8',
ListTypeGroupDescription: 'ListTypeGroupDescription 8',
ParentKey: '34bcdc74-ab42-4538-8657-0a2b0473fcb8',
__metadata: {}
}
]
}
}
];
//This is the values which I should search find in oDataSet
//The to_ListTypeGroupAssignment or other property which under the same structure
//should be with the following path but under the results which is the only
//hardcoded property
var aProperties = [
"to_ListTypeGroupAssignment/ListTypeGroup",
"to_ListTypeGroupAssignment/ListTypeGroupDescription"
]
这是输出
这是应该从上面输入中的两个对象的合并构建的输出示例
var aSelectedDataSet = [
{
__metadata: {
aaa: 111,
bbb: 222
},
to_ListTypeGroupAssignment: {
results: [
{
ListTypeGroup: 'ListTypeGroup1',
ListTypeGroupDescription: 'ListTypeGroupDescription 1'
},
{
ListTypeGroup: 'ListTypeGroup2',
ListTypeGroupDescription: 'ListTypeGroupDescription 2',
},
{
ListTypeGroup: 'ListTypeGroup3',
ListTypeGroupDescription: 'ListTypeGroupDescription 3',
}
]
}
},
{
__metadata: {
aaa: 333,
bbb: 444
},
to_ListTypeGroupAssignment: {
results: [
{
ListTypeGroup: 'ListTypeGroup1',
ListTypeGroupDescription: 'ListTypeGroupDescription 1'
},
{
ListTypeGroup: 'ListTypeGroup2',
ListTypeGroupDescription: 'ListTypeGroupDescription 2',
},
{
ListTypeGroup: 'ListTypeGroup3',
ListTypeGroupDescription: 'ListTypeGroupDescription 3',
}
]
}
}
]
只是为了从下面的 cmets 中澄清:)
唯一可以硬编码的是results。
不是任何属性名称,例如 ListTypeGroup& ListTypeGroupDescription
这应该是 generic 并从 aProperties 读取
你看 oData 的结构应该像下面这样
property(like -> to_ListTypeGroupAssignmen)
results(hardcoded & mandatory in every exist object)
properties(like ListTypeGroup& ListTypeGroupDescription with there values)
如果我需要更清楚,请告诉我如何,我应该添加哪些附加信息...这是在我尽可能更新问题之后...
【问题讨论】:
-
你能用代码替换图像吗?人们尝试复制和粘贴代码并重建您的问题要容易得多,而不是从图像中读取它。见How to Ask。
-
学习编程的一部分就是自己编程。您如何尝试将相关问题应用到您自己的代码中,然后针对您在哪里遇到问题提出一个具体问题?
-
@MiltoxBeyond - 只有结果可以硬编码 :) 忘了提这个
-
@Frederik.L - 谢谢,但我认为你错过了这里的重点......大多数 cmets 都是针对我今天大幅更新并创建 JsFiddle 的原始问题,输出应该是我放的 aSelectedDataSet以及应该如何创建它的示例(使用值),此外,在小提琴的底部,您可以看到我尝试的内容,但没有成功:(
-
很抱歉,但我必须坚持:你一直在问你需要什么,所以即使有善意,也没有人能真正理解它。你似乎总是很匆忙,这不是达到目标的好方法。因此,请花时间以清晰的方式重构您的问题!否则,您想要节省的时间实际上会浪费在您身上(因为您不会得到任何答案)......对我们来说也是如此!
标签: javascript jquery arrays recursion javascript-objects