How could i get Not matched String in javascript?
Welcome to Programming Tutorial official website. Today - we are going to cover how to solve / find the solution of this error How could i get Not matched String in javascript? on this date .
I have a booking array and I have to search inside the array using searchValue
.
here we have to check the booking id field. if booking id and searchValue
matched we have to push that object into the result array.
Working code
result array example below
let searchValue = “12,13,15”
Result:
[{ name:"user 3", bookingid:12, product: "ui" }, { name:"user 4", bookingid:13, product: "ef" }]
Expected Output:
12,13 matched in booking array so we have to get NotMatchedsearchValue = “15” could you please help here
let bookingArr = [ { name:"user 1", bookingid:10, product: "ab" }, { name:"user 1", bookingid:10, product: "cd" }, { name:"user 2", bookingid:11, product: "ui" }, { name:"user 1", bookingid:10, product: "ef" }, { name:"user 3", bookingid:12, product: "ui" }, { name:"user 4", bookingid:13, product: "ef" }, ]; let searchValue = "12,13,15"; let set = new Set(searchValue.split(",").map(Number)); // for faster lookup let res = bookingArr.filter(x => set.has(x.bookingid)); console.log(res); // how can i get not matched searchValue // expected result notmatchedsearchValue ="15"
Answer
The situation here is different when compared to your previous question. So, here you need to make a Set
out bookingArr
and not out of searchVal
.
let bookingArr = [ { name: "user 1", bookingid: 10, product: "ab" }, { name: "user 1", bookingid: 10, product: "cd" }, { name: "user 2", bookingid: 11, product: "ui" }, { name: "user 1", bookingid: 10, product: "ef" }, { name: "user 3", bookingid: 12, product: "ui" }, { name: "user 4", bookingid: 13, product: "ef" }, ]; let searchValue = "12,13,15"; let set = new Set(bookingArr.map((b) => b.bookingid)); let res = searchValue .split(",") .map(Number) .filter((s) => !set.has(s)) .join(); console.log(res);