我有一个字符串存在于数组中,如下所示
getid Array [
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2",
]
,我想把这个部分E8R52y6dD8XI3shXhnhS9pVzUpe2
排除在外
实际上E8R52y6dD8XI3shXhnhS9pVzUpe2
这是user.uid。这是两个用户id组合id"E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2"
此组合id存在于数组中。这个数组中将有这么多组合id。我想从所有联合id中排除user.uidE8R52y6dD8XI3shXhnhS9pVzUpe2
。如果排除user.uid,则此数组中将保留一个id部分。我想让id值的剩余部分使用。
let userid = E8R52y6dD8XI3shXhnhS9pVzUpe2
let combineId = ["E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2","E8R52y6dD8XI3shXhnhS9pVzUpe26Lh5bHrzCSvI2bmoMv7KuduMGwe2","E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2"]
// this is value what i want
let remainIdinArray = ["6Lf5bHwzCSbI2bmoMv7KuduMGwe2","6Lh5bHrzCSvI2bmoMv7KuduMGwe2","6Lf5bHwzCSbI2bmoMv7KuduMGwe2"]
怎么能做到。感谢
1(您只需循环并用''
(空字符串(replace
和userId
,就会得到您期望的
let userid = "E8R52y6dD8XI3shXhnhS9pVzUpe2";
let combineId = [
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2",
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lh5bHrzCSvI2bmoMv7KuduMGwe2",
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2",
];
const result = combineId.map((id) => id.replace(userid, ""));
console.log(result);
2(您也可以使用slice
、indexOf
来获得所需的结果:
let userid = "E8R52y6dD8XI3shXhnhS9pVzUpe2";
let combineId = [
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2",
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lh5bHrzCSvI2bmoMv7KuduMGwe2",
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2",
];
const result = combineId.map((id) => id.slice(id.indexOf(userid) + userid.length));
console.log(result);
您可以使用贴图和切片!
let userid = "E8R52y6dD8XI3shXhnhS9pVzUpe2";
let combineId = [
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2",
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lh5bHrzCSvI2bmoMv7KuduMGwe2",
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2"
];
let remainIdinArray = combineId.map((id) => id.slice(-userid.length));
console.log(remainIdinArray);