I have string that exist in array as follow
getid Array [
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2",
]
I want to exclude this part E8R52y6dD8XI3shXhnhS9pVzUpe2
from that ,
actually E8R52y6dD8XI3shXhnhS9pVzUpe2
this is user.uid .
this is two user id combine id "E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2"
this combine id exist in arrays.
so many combine id will has in this array.
I want to exclude user.uid E8R52y6dD8XI3shXhnhS9pVzUpe2
from all combine id .
if exclude user.uid , there will be remain one id part in this array.
I want to get this remain part of id value to use.
let userid = E8R52y6dD8XI3shXhnhS9pVzUpe2
let combineId = ["E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2","E8R52y6dD8XI3shXhnhS9pVzUpe26Lh5bHrzCSvI2bmoMv7KuduMGwe2","E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2"]
// this is value what i want
let remainIdinArray = ["6Lf5bHwzCSbI2bmoMv7KuduMGwe2","6Lh5bHrzCSvI2bmoMv7KuduMGwe2","6Lf5bHwzCSbI2bmoMv7KuduMGwe2"]
how can do that. thanks
CodePudding user response:
1) You can just loop over and replace
the userId
with ''
(empty string) and you will get what you are expecting
let userid = "E8R52y6dD8XI3shXhnhS9pVzUpe2";
let combineId = [
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2",
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lh5bHrzCSvI2bmoMv7KuduMGwe2",
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2",
];
const result = combineId.map((id) => id.replace(userid, ""));
console.log(result);
2) You can also use slice
, indexOf
to get the desired result as:
let userid = "E8R52y6dD8XI3shXhnhS9pVzUpe2";
let combineId = [
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2",
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lh5bHrzCSvI2bmoMv7KuduMGwe2",
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2",
];
const result = combineId.map((id) => id.slice(id.indexOf(userid) userid.length));
console.log(result);
CodePudding user response:
You can use the map and the slice!
let userid = "E8R52y6dD8XI3shXhnhS9pVzUpe2";
let combineId = [
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2",
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lh5bHrzCSvI2bmoMv7KuduMGwe2",
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2"
];
let remainIdinArray = combineId.map((id) => id.slice(-userid.length));
console.log(remainIdinArray);