如何从Javascript或AngularJS中的字符串数组中删除一组特定的字符,使其不重复?

时间:2023-02-07 07:41:53

Say I have an array as such;

假设我有一个这样的数组;

$scope.array = ["ABC", "ABCDEF", "ABCDEFGHI", "ABCAFGKJA"];

is it possible to return it as such?;

是否有可能归还?

$scope.array = ["ABC", "DEF", "GHI", "KJ"];

sorry if the question is not too clear, still learning the terminologies. cheers!

如果问题不太清楚,请继续学习术语。干杯!

3 个解决方案

#1


2  

A version with String.prototype.replace() and Array#reduce, for inner and outer loop.

一个包含String.prototype.replace()和Array#reduce的版本,用于内部和外部循环。

In the outer loop, it is necessary to iterate the actual reduced items, to reduce the actual string.

在外部循环中,需要迭代实际减少的项,以减少实际的字符串。

function getParts(r, a) {
    r.push(r.reduce(function (q, b) {
        return q.replace(b, '');
    }, a));
    return r;
}

console.log(["ABC", "ABCDEF", "ABCDEFGHI"].reduce(getParts, []));
console.log(["A|B|C", "(A|B|C)&D"].reduce(getParts, []));
console.log(["abc", "abch", "def", "abchdefg"].reduce(getParts, []));
console.log(["A|B|C", "(A|B|C)&D", "E|F|G", "((A|B|C)&D)&(E|F|G)"].reduce(getParts, []));
.as-console-wrapper { max-height: 100% !important; top: 0; }

#2


0  

$(document).ready(function(){ 
var $scope = { array: ["ABC", "ABCDEF", "ABCDEFGHI"] };
var result = $scope.array.map(function (a, i, b) {
        return a.indexOf(b[i - 1]) ? a : a.slice(b[i - 1].length - a.length);
    });

$('#results').text(result);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="results"></div>

#3


0  

Taking your comments into account from other answers, if you want to remove all occurrences of the characters no matter where in the string they are positioned, the following should work.

从其他答案中考虑您的评论,如果您想删除所有出现的字符,无论它们位于字符串的什么位置,以下内容应该是有效的。

Yes, there is most likely a way more efficient way of writing this but the below seems to work.

是的,很有可能有一种更有效的方法来写这篇文章,但是下面的方法似乎是可行的。

var x = ["A|B|C", "(A|B|C)&D", "E|F|G", "((A|B|C)&D)&(E|F|G)"];
// Expected Result  ["A|B|C", "D", "E|F|G", ""]
var existingChars = [];
var reg;

for (var i = 0; i < x.length; i++) {
  for (var j = 0; j < existingChars.length; j++) {
    existingChars[j] = existingChars[j].replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
    reg = new RegExp(existingChars[j], 'g');
    x[i] = x[i].replace(reg,'').replace(/[&\)\(]+/g, '');
  }
  
  existingChars = existingChars.concat(x[i].split(''));
}

console.log(x);

#1


2  

A version with String.prototype.replace() and Array#reduce, for inner and outer loop.

一个包含String.prototype.replace()和Array#reduce的版本,用于内部和外部循环。

In the outer loop, it is necessary to iterate the actual reduced items, to reduce the actual string.

在外部循环中,需要迭代实际减少的项,以减少实际的字符串。

function getParts(r, a) {
    r.push(r.reduce(function (q, b) {
        return q.replace(b, '');
    }, a));
    return r;
}

console.log(["ABC", "ABCDEF", "ABCDEFGHI"].reduce(getParts, []));
console.log(["A|B|C", "(A|B|C)&D"].reduce(getParts, []));
console.log(["abc", "abch", "def", "abchdefg"].reduce(getParts, []));
console.log(["A|B|C", "(A|B|C)&D", "E|F|G", "((A|B|C)&D)&(E|F|G)"].reduce(getParts, []));
.as-console-wrapper { max-height: 100% !important; top: 0; }

#2


0  

$(document).ready(function(){ 
var $scope = { array: ["ABC", "ABCDEF", "ABCDEFGHI"] };
var result = $scope.array.map(function (a, i, b) {
        return a.indexOf(b[i - 1]) ? a : a.slice(b[i - 1].length - a.length);
    });

$('#results').text(result);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="results"></div>

#3


0  

Taking your comments into account from other answers, if you want to remove all occurrences of the characters no matter where in the string they are positioned, the following should work.

从其他答案中考虑您的评论,如果您想删除所有出现的字符,无论它们位于字符串的什么位置,以下内容应该是有效的。

Yes, there is most likely a way more efficient way of writing this but the below seems to work.

是的,很有可能有一种更有效的方法来写这篇文章,但是下面的方法似乎是可行的。

var x = ["A|B|C", "(A|B|C)&D", "E|F|G", "((A|B|C)&D)&(E|F|G)"];
// Expected Result  ["A|B|C", "D", "E|F|G", ""]
var existingChars = [];
var reg;

for (var i = 0; i < x.length; i++) {
  for (var j = 0; j < existingChars.length; j++) {
    existingChars[j] = existingChars[j].replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
    reg = new RegExp(existingChars[j], 'g');
    x[i] = x[i].replace(reg,'').replace(/[&\)\(]+/g, '');
  }
  
  existingChars = existingChars.concat(x[i].split(''));
}

console.log(x);