I have a strange problem where my array filter works on all values within an array, except when the string value contains a mixture of numbers and letters. At least that's what I think is the issue.
Please see the two examples below. In the first case I am looking for records where card_id
has a value cardid-1602670475144
. In this case the filter array is always empty.
let arr = [{"_id":{"$oid":"5f8b5f2f32d764f7ad4c450b"},"card_id":"cardid-1602670475144","brand":"National Treasures","card_number":{"$numberDouble":"100"},"career_stage":"Veteran","forTrade":"Yes","image_path":"https://res.cloudinary.com/dkwmxhsem/image/upload/t_Card Resize/v1599758190/","manufacturer":"Panini","number":{"$numberDouble":"16"},"player":"Trent Williams","print_run":{"$numberDouble":"25"},"purchase_date":"Thu Aug 20 00:00:00 GMT+01:00 2020","series":"Century Materials","status":"Own","team":"Washington Football Team","type":"Patch","user":"[email protected]","variation":"Holo Silver"},{"_id":{"$oid":"5f8b5f2f32d764f7ad4c4537"},"card_id":"cardid-1602670491958","brand":"Playbook","card_number":{"$numberDouble":"85"},"career_stage":"Rookie Card","forTrade":"","image_path":"https://res.cloudinary.com/dkwmxhsem/image/upload/t_Card Resize/v1599758190/","manufacturer":"Panini","number":{"$numberDouble":"80"},"player":"Ryan Kerrigan","print_run":{"$numberDouble":"299"},"purchase_date":"Tue Sep 01 00:00:00 GMT+01:00 2020","series":"Rookie Signatures","status":"Own","team":"Washington Football Team","type":"Auto","user":"[email protected]","variation":"Base","year":"2011"},{"_id":{"$oid":"5f8b5f3032d764f7ad4c455f"},"card_id":"cardid-1602670496512","brand":"Contenders","card_number":{"$numberDouble":"144"},"career_stage":"Rookie Card","forTrade":"Yes","image_path":"https://res.cloudinary.com/dkwmxhsem/image/upload/t_Card Resize/v1599758190/","manufacturer":"Panini","number":{"$numberDouble":"0"},"player":"Jawan Jamison","print_run":{"$numberDouble":"21"},"purchase_date":"Mon Sep 14 00:00:00 GMT+01:00 2020","series":"Rookie Ticket Autographs","status":"Own","team":"Washington Football Team","type":"Auto","user":"[email protected]","variation":"Cracked Ice","year":"2013"}]
function filterCards(cardsArray, path, cardProperty) {
console.log("ORIGINAL ARRAY " + JSON.stringify(cardsArray));
if (path === "want" || "own") {
path = path[0].toUpperCase() + path.substring(1);
}
var filtered = Object.values(cardsArray).filter(function (card) {
if (card[cardProperty] === path) {
return true;
}
});
console.log("FILTERED ARRAY " + JSON.stringify(filtered));
return filtered;
}
var newArray = filterCards(
arr,
"cardid-1602670475144",
"card_id",
);
In the second case I modify the data so that card_id
has a value 1602670475144
(i.e. I have removed the cardid-
prefix) and look for that. Then the correct data is returned.
let arr = [{"_id":{"$oid":"5f8b5f2f32d764f7ad4c450b"},"card_id":"1602670475144","brand":"National Treasures","card_number":{"$numberDouble":"100"},"career_stage":"Veteran","forTrade":"Yes","image_path":"https://res.cloudinary.com/dkwmxhsem/image/upload/t_Card Resize/v1599758190/","manufacturer":"Panini","number":{"$numberDouble":"16"},"player":"Trent Williams","print_run":{"$numberDouble":"25"},"purchase_date":"Thu Aug 20 00:00:00 GMT+01:00 2020","series":"Century Materials","status":"Own","team":"Washington Football Team","type":"Patch","user":"[email protected]","variation":"Holo Silver"},{"_id":{"$oid":"5f8b5f2f32d764f7ad4c4537"},"card_id":"cardid-1602670491958","brand":"Playbook","card_number":{"$numberDouble":"85"},"career_stage":"Rookie Card","forTrade":"","image_path":"https://res.cloudinary.com/dkwmxhsem/image/upload/t_Card Resize/v1599758190/","manufacturer":"Panini","number":{"$numberDouble":"80"},"player":"Ryan Kerrigan","print_run":{"$numberDouble":"299"},"purchase_date":"Tue Sep 01 00:00:00 GMT+01:00 2020","series":"Rookie Signatures","status":"Own","team":"Washington Football Team","type":"Auto","user":"[email protected]","variation":"Base","year":"2011"},{"_id":{"$oid":"5f8b5f3032d764f7ad4c455f"},"card_id":"cardid-1602670496512","brand":"Contenders","card_number":{"$numberDouble":"144"},"career_stage":"Rookie Card","forTrade":"Yes","image_path":"https://res.cloudinary.com/dkwmxhsem/image/upload/t_Card Resize/v1599758190/","manufacturer":"Panini","number":{"$numberDouble":"0"},"player":"Jawan Jamison","print_run":{"$numberDouble":"21"},"purchase_date":"Mon Sep 14 00:00:00 GMT+01:00 2020","series":"Rookie Ticket Autographs","status":"Own","team":"Washington Football Team","type":"Auto","user":"[email protected]","variation":"Cracked Ice","year":"2013"}]
function filterCards(cardsArray, path, cardProperty) {
console.log("ORIGINAL ARRAY " + JSON.stringify(cardsArray));
if (path === "want" || "own") {
path = path[0].toUpperCase() + path.substring(1);
}
var filtered = Object.values(cardsArray).filter(function (card) {
if (card[cardProperty] === path) {
return true;
}
});
console.log("FILTERED ARRAY " + JSON.stringify(filtered));
return filtered;
}
var newArray = filterCards(
arr,
"1602670475144",
"card_id"
);
Can anyone advise what I'm doing wrong here; I feel like I must be making some stupid mistake!
The issue is you're searching for the number exactly. When cardid- is in front, it won't find anything because nothing matches exactly. I recommend you search for cardid-1602670475144 instead of 1602670475144. This matches correctly.
(I updated the code snippet to be more concise and use ES6--I recommend you use it if possible)