如何在复杂JavaScript数组对象中检索?指令代码调试疑问
Hey Naomi, let's work through your questions one by one—first that confusing this.Panes behavior, then how to search through complex JS objects/arrays.
First: What's going on with this.Panes?
Looking at your code, I suspect you initialized this.Panes as an array (like this.Panes = []), but you're assigning values using string keys (pane.name). Here's the thing: in JavaScript, arrays are just special objects under the hood, so you can add custom string-named properties to them—but those aren't actual array elements.
When you see this.Panes[name1] = paneObject in the debugger, those are object properties, not items in the array. They won't show up in array methods like forEach() or affect the length property. This is almost certainly not what you want if you're trying to store panes by name.
Fix this by using a plain object or Map instead:
Initialize this.Panes as an empty object (or Map for more advanced key handling):
// Inside your directive's constructor/initialization this.Panes = {}; // OR this.Panes = new Map();
Then update your addPane method to match:
NavTabsDirective.prototype.addPane = function (pane) { if (!_.isObject(pane) || "Pane" !== pane.constructor.name) { throw new TypeError("pane must be an instance of Pane"); } if (_.isUndefined(this.FirstPane)) { this.FirstPane = pane; } // For plain object this.Panes[pane.name] = pane; // For Map: this.Panes.set(pane.name, pane); };
This makes your intent clear (storing panes in a key-value structure) and avoids confusion between arrays and objects.
Second: Searching through complex JavaScript objects/arrays
Let's cover common scenarios based on how you're storing your panes:
1. Searching a key-value object (like your fixed this.Panes)
- Get a pane by name directly: Use the key if you know it:
const userPane = this.Panes["user-profile"]; // For Map: const userPane = this.Panes.get("user-profile"); - Find a pane that matches a condition: Convert the object's values to an array first, then use array methods:
// Find the first pane that's active const activePane = Object.values(this.Panes).find(pane => pane.isActive); // Get all panes that are hidden const hiddenPanes = Object.values(this.Panes).filter(pane => pane.isHidden); // Check if any pane has a specific title const hasHomePane = Object.values(this.Panes).some(pane => pane.title === "Home");
2. Searching an array of objects
If you have a standard array (e.g., const paneArray = Object.values(this.Panes)), use these built-in methods:
find(): Returns the first matching element (orundefined)filter(): Returns all matching elements in a new arraysome(): Returnstrueif at least one element matchesfindIndex(): Returns the index of the first matching element (or-1)
Example:
// Find the index of the first disabled pane const disabledIndex = paneArray.findIndex(pane => pane.isDisabled);
3. Searching nested structures
If your panes have child panes or nested data, use a recursive function to dig through the structure:
function findPaneByTitle(panes, targetTitle) { for (const pane of Object.values(panes)) { if (pane.title === targetTitle) return pane; // Recurse into child panes if they exist if (pane.childPanes) { const foundChild = findPaneByTitle(pane.childPanes, targetTitle); if (foundChild) return foundChild; } } return null; // Return null if no match found } // Usage: const settingsPane = findPaneByTitle(this.Panes, "Settings");
内容的提问来源于stack exchange,提问作者Naomi

