将数组复制到变量,同时排除项-性能

Copying an array to a variable whilst excluding items - Performance

本文关键字:排除 性能 数组 复制 变量      更新时间:2023-09-26

这里有一个数组-

var children = [Group, Path,Path, CompoundPath,Path];

我想复制数组到一个变量中,我像这样做

var selectionItems = children.slice();

现在我想在复制数组到变量时,在selectionItems中只保留Path的项目。

我已经尝试了splice()方法,但我认为它弄乱了复制数组的索引,这是我不希望发生的事情。我也不知道如何在数组的新副本中排除除Path以外的任何其他内容。

您可以使用.filter:

var selectionItems = children.filter(function (el) { return el instanceof Path; });