gpt4 book ai didi

javascript - 将一个数组与另一个数组值拼接

转载 作者:行者123 更新时间:2023-12-03 05:47:00 24 4
gpt4 key购买 nike

我有一个函数 displaySelectedRole() ,我有变量 $scope.Role$scope.rolenames 。我需要删除所有$scope.rolename 中的 $scope.role 中可用的值。

$scope.role= ["A","B","C"]; 

$scope.rolename =["A","B","C","D","E"]

我需要拼接这些值并得到$scope.rolename = ["D","E"]

$scope.displaySelectedRole = function(role, index) {
debugger;
$scope.role.splice(RoleNames[index]);
console.log($scope.role);

我尝试使用基于索引的拼接,但问题是它在控制台中给出了空数组值。

最佳答案

您可以使用filter

var $scope = {};  // Ignore this line
$scope.role= ["A","B","C"];
$scope.rolename = ["A","B","C","D","E"];

$scope.rolename = $scope.rolename.filter(function(role){
return $scope.role.indexOf(role) === -1;
})

console.log($scope.rolename);

如果您想直接删除它们,您可以迭代 $scope.role 并使用 splice

   var $scope = {};  // Ignore this line
$scope.role= ["A","B","C"];
$scope.rolename = ["A","B","C","D","E"];

$scope.role.forEach(function(role){
var index = $scope.rolename.indexOf(role);
if(index !== -1) $scope.rolename.splice(index, 1);
})

console.log($scope.rolename);

注意: Array.filter 将返回一个新数组,与 array.splice 不同,它会修改原始数组。

引用

关于javascript - 将一个数组与另一个数组值拼接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40302038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com