My problem . I have an object like this:
我的问题 。我有一个像这样的对象:
t.mainobject = {
prop1: "",
prop2: "",
id: "1",
prop3: [
child1: {
id: "111"
},
child2: {
id: "112"
}
]
};
only child1/child2 is visible to me whereas I donot know its parent object . I tried using underscore but have not found any method which can provide me the parent object .
只有child1 / child2对我来说是可见的,而我不知道它的父对象。我尝试使用下划线但没有找到任何可以为我提供父对象的方法。
thanks in advance for any advice/suggestion.
提前感谢任何建议/建议。
edit1:
all my child objects were created from multiple object so it end up as a flatten object.
edit1:我的所有子对象都是从多个对象创建的,因此它最终成为一个展平对象。
like:
喜欢:
t.mainobject = {
prop1: "",
prop2: "",
id: "1",
prop3: [
child1: {
id: "111"
},
child2: {
id: "112"
}
]
};
t.mainobject1 = {
prop1: "",
prop2: "",
id: "2",
prop3: [
child1: {
id: "211"
},
child2: {
id: "212"
}
]
};
t.mainobject3 = {
prop1: "",
prop2: "",
id: "1",
prop3: [
child1: {
id: "311"
},
child2: {
id: "312"
}
]
};
Now I ended up with an array with :
现在我最终得到了一个数组:
[{id:"111"},{id:"112"}, {id:"211"},{id:"212"}, {id:"311"},{id:"312"}]
Now how can I able to get the parent object containing the child object using the id .
现在我如何能够使用id获取包含子对象的父对象。
edit 2:
编辑2:
my solution :
我的解决方案
var parent = _.filter(allParent, function(r){
for(var i=0; i<r.posts.length; i++){
if(r.posts[i].id === allChildPosts[index].id){
return r.id;
}
}
});
it return me the parent object and now I can easily access any property.
它返回父对象,现在我可以轻松访问任何属性。
1 个解决方案
#1
2
This is not directly possible, because as far as JavaScript knows, child1 could be a property of any number of objects: therefore, the concept of a 'parent object' does not really make sense generally speaking.
这不是直接可能的,因为就JavaScript而言,child1可能是任意数量对象的属性:因此,“父对象”的概念在一般情况下并不真正有意义。
What's more, if there are no references to the parent objects, they will be garbage-collected, so no hope to get back to them.
更重要的是,如果没有对父对象的引用,它们将被垃圾收集,因此没有希望回到它们。
However, if you can have a list of all the potential parents of an object, you can search them all :
但是,如果您可以列出对象的所有潜在父项,则可以全部搜索它们:
function findParent(child){
for(var i=0; i < parents.length; i++){
var mainobject = parents[i]; // a potential parent
for(childName in mainobject.prop3){
if(mainobject.prop3[childName].id === child.id){ // match found!
return mainobject;
}
}
}
}
#1
2
This is not directly possible, because as far as JavaScript knows, child1 could be a property of any number of objects: therefore, the concept of a 'parent object' does not really make sense generally speaking.
这不是直接可能的,因为就JavaScript而言,child1可能是任意数量对象的属性:因此,“父对象”的概念在一般情况下并不真正有意义。
What's more, if there are no references to the parent objects, they will be garbage-collected, so no hope to get back to them.
更重要的是,如果没有对父对象的引用,它们将被垃圾收集,因此没有希望回到它们。
However, if you can have a list of all the potential parents of an object, you can search them all :
但是,如果您可以列出对象的所有潜在父项,则可以全部搜索它们:
function findParent(child){
for(var i=0; i < parents.length; i++){
var mainobject = parents[i]; // a potential parent
for(childName in mainobject.prop3){
if(mainobject.prop3[childName].id === child.id){ // match found!
return mainobject;
}
}
}
}