从两个列表中列出一个列表

Make a list from two lists

本文关键字:列表 一个 两个      更新时间:2023-09-26

我有两个列表,其中包含相同的元素和不同的元素。如何从这两个列表中列出一个列表,并向我展示共性和新项目?

...
            <list-of-items items="application.units"
                           label="$item.name"
                           selected-item="unit"
                           selectable="true"
                           onedit="on_edit_unit($item)"
                           editable="false"
                           size="small">
            </list-of-items>
            <list-of-items items="applicationCible.units"
                           label="$item.name"
                           selected-item="unit"
                           selectable="true"
                           onedit="on_edit_unit($item)"
                           editable="false"
                           size="small">
            </list-of-items>
...

更新

在我的HTML中:

..
          <list-of-items items="platforms.concat(platformsCible).unique()"
                           label="$item"
                           selected-item="platform"
                           createfunction="add_platform($name)"
                           selectable="true"
                           editable="false"
                           size="small">
            </list-of-items>
..

在我的JS中:

propertiesModule.controller('PropertiesCtrl', ['$scope', '$routeParams', 'PropertiesService', 'ApplicationService', 'PlatformService', 'Page',  function ($scope, $routeParams, PropertiesService, ApplicationService, PlatformService, Page) {
...
Array.prototype.unique = function() {
    var a = this.concat();
    for(var i=0; i<a.length; ++i) {
        for(var j=i+1; j<a.length; ++j) {
            if(a[i] === a[j])
                a.splice(j--, 1);
        }
    }
    return a;
};
}]);

此方法存在重复项

实现这一点的一种方法是使用Array.prototype.concat():将两个列表连接起来

<list-of-items items="application.units.concat(applicationCible.units)"
               label="$item.name"
               selected-item="unit"
               selectable="true"
               onedit="on_edit_unit($item)"
               editable="false"
               size="small">
</list-of-items>
相关文章: