I have a booking array of arrays that contain objects and I have to search inside the array using searchValue
could you please help me here
here we have to check the booking id if booking id and searchValue
matched we have to push that object into result array.
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" } ] ]; var searchValue = "10,11"; var FOUND = bookingArr.find(function(post, index) { if (post.bookingid == 11) return true; }); console.log(FOUND)
expected result
[ { name:"user 1", bookingid:10, product: "ab" },
{ name:"user 1", bookingid:10, product: "cd" },
{ name:"user 1", bookingid:10, product: "ef" },
{ name:"user 2", bookingid:11, product: "ui" }]
You can try this code:
const 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' }, ]]; const searchValue = '10,11'; const sv = searchValue.split(',').map(Number); const FOUND = bookingArr.flat().filter(({ bookingid }) => sv.includes(bookingid)); console.log(FOUND);
You can try this:
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' }, ], ]; var searchValue = '10,11'; var searchIds = searchValue.split(','); var FOUND = []; bookingArr.forEach((el) => { el.forEach((el2) => { searchIds.forEach((num) => { if (num == el2.bookingid) { FOUND.push(el2); } }); }); }); console.log(FOUND);
Following with filter
and Set
will work:-
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" } ] ]; var searchValue = "10,11"; function logic(arr, searchValue) { let searchInputs = searchValue.split(','); let searchSet = new Set(searchInputs.map(val => parseInt(val))); let output = bookingArr.flat().filter(curr => searchSet.has(curr.bookingid)) return output; } console.log(logic(bookingArr, searchValue));
You can use Array#filter
with a Set
for better performance after flattening the array.
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 = "10,11"; let set = new Set(searchValue.split(",").map(Number)); // for faster lookup let res = bookingArr.flat().filter(x => set.has(x.bookingid)); console.log(res);
You can simply flatten the array and filter out the desired results. Also, I've create a Set
out of the search value for making the code more performant.
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' }, ], ]; const searchValue = "10,11" const searchSet = new Set(searchValue.split(",")) const res = bookingArr.flat().filter(b => searchSet.has(String(b.bookingid))) console.log(res)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.