patternjavascriptMinor
Select grand child in array knockout
Viewed 0 times
arraygrandknockoutchildselect
Problem
I'm trying to select a grand child from an array in an array.
If it was C# I would have wrote something like below
I have code that works, but I'm not sure it is the best way to do it:
Is there a method I can use that I've never heard of perhaps?
If it was C# I would have wrote something like below
SubSteps.Select(item => item.First(subitem => subitem == 1)).First();I have code that works, but I'm not sure it is the best way to do it:
ko.utils.arrayFirst(ko.utils.arrayFirst(self.SubSteps(),
function (item) {
return ko.utils.arrayFirst(item.SubSteps(),
function (subItem) { return subItem.InternalName == localStorage.getItem('lastWizardPage'); });
}).SubSteps(), function (item) {
return item.InternalName == localStorage.getItem('lastWizardPage');
})Is there a method I can use that I've never heard of perhaps?
Solution
Interesting question.
First off, your indentation is terrible, your code is far more readable like this:
Second, you are retrieving your subitem twice, because you loose track of it, simply use a 'local global' and assign your subitem to it.
or, if you dont mind some sorcery (don't do this at work!)
Furthemore, you should consider caching
First off, your indentation is terrible, your code is far more readable like this:
ko.utils.arrayFirst(ko.utils.arrayFirst(self.SubSteps(),
function(item) {
return ko.utils.arrayFirst(item.SubSteps(),
function(subItem) {
return subItem.InternalName == localStorage.getItem('lastWizardPage');
});
}).SubSteps(), function(item) {
return item.InternalName == localStorage.getItem('lastWizardPage');
})Second, you are retrieving your subitem twice, because you loose track of it, simply use a 'local global' and assign your subitem to it.
var needle, found;
ko.utils.arrayFirst(self.SubSteps(),
function(item) {
return ko.utils.arrayFirst(item.SubSteps(),
function(subItem) {
found = subItem.InternalName == localStorage.getItem('lastWizardPage');
if(found) {
needle = subItem;
}
return found;
});
});or, if you dont mind some sorcery (don't do this at work!)
var needle, found;
ko.utils.arrayFirst(self.SubSteps(),
function(item) {
return ko.utils.arrayFirst(item.SubSteps(),
function(subItem) {
found = subItem.InternalName == localStorage.getItem('lastWizardPage');
return found && (needle = subItem), found;
});
});Furthemore, you should consider caching
localStorage.getItem('lastWizardPage') and you should consider implementing old skool loops so that you can exit immediately.Code Snippets
ko.utils.arrayFirst(ko.utils.arrayFirst(self.SubSteps(),
function(item) {
return ko.utils.arrayFirst(item.SubSteps(),
function(subItem) {
return subItem.InternalName == localStorage.getItem('lastWizardPage');
});
}).SubSteps(), function(item) {
return item.InternalName == localStorage.getItem('lastWizardPage');
})var needle, found;
ko.utils.arrayFirst(self.SubSteps(),
function(item) {
return ko.utils.arrayFirst(item.SubSteps(),
function(subItem) {
found = subItem.InternalName == localStorage.getItem('lastWizardPage');
if(found) {
needle = subItem;
}
return found;
});
});var needle, found;
ko.utils.arrayFirst(self.SubSteps(),
function(item) {
return ko.utils.arrayFirst(item.SubSteps(),
function(subItem) {
found = subItem.InternalName == localStorage.getItem('lastWizardPage');
return found && (needle = subItem), found;
});
});Context
StackExchange Code Review Q#63840, answer score: 2
Revisions (0)
No revisions yet.