im building an api which needs to find the longest path that matches a number in a very performant manner.
eg
// API Request
{
'number': '123456789'
}
// DATA
[
{
'prefix': '1',
'price': 30.5
},
{
'prefix': '123',
'price': 10.5
},
{
'prefix': '12345',
'price': 22.5
},
]
// API RESPONSE
{
'prefix': '12345',
'price': 22.5
},
As you can see from above the response should be the row with prefix of 12345 as it is the longest. please i need a bit of help in doing this. i have spent about 2 days now looking for a solution so i decided to come to stack overflow for answers. Thanks in advance!
CodePudding user response:
You could do the following where you check each characters position in the prefix to figure out which data set is the best match.
const incoming = {
'number': '123456789'
}
const data = [{
'prefix': '1',
'price': 30.5
},
{
'prefix': '123',
'price': 10.5
},
{
'prefix': '12345',
'price': 22.5
}
];
let bestMatch = {
matchSuccess: 0,
data: data[0]
};
for (let i = 0; i < data.length; i ) {
let matchSuccess = 0;
for (var x = 0; x < data[i].prefix.length; x ) {
const c = data[i].prefix.charAt(x);
if (data[i].prefix.charAt(x) === incoming.number.charAt(x)) {
matchSuccess ;
}
}
if (matchSuccess > bestMatch.matchSuccess) {
bestMatch = {
matchSuccess,
data: data[i]
}
}
}
console.log(bestMatch);
CodePudding user response:
From the above comment ...
"The OP is not looking for ... "the longest possible path from an array of objects" ... which hopefully not only to me means a result like ...
'data[2].prefix'
... for ...const incoming = { prefix: '123456789' }
. The OP's providedincoming
value even would not match anything due to thenumber: '123456789'
key-value pair (btw.number
being a string type) instead ofprefix: '123456789'
. I highly recommend to edit topic and description of the problem."
But what the OP actually might want is ... filter, from an array of objects, the very first object where any of the object entry's stringified values matches the stringified value of the API call in the longest possible way.
function collectItemOfBestCoveringEntryValue(collector, item) {
const { search = '', coverage = 0, result = null } = collector;
if (search !== '') {
const matchingValues = Object
// retrieve all of an item's values.
.values(item)
// filter any value which matches `search`.
.filter(value => {
value = String(value);
return ((
value !== ''
) && (
// it might even goe both ways ...
// ... `value` in `search` ...
search.includes(value) ||
// ... or `search` in `value`.
value.includes(search)
));
});
// retrieve the longest (stringified) value's length.
const bestCoverage = String(
matchingValues
.sort((a, b) => b.length - a.length)[0] ?? ''
).length;
if (bestCoverage > coverage) {
collector.coverage = bestCoverage;
collector.result = item;
}
}
return collector;
}
const serverSideData = [{
'prefix': '1',
'price': 30.5,
}, {
'prefix': '123',
'price': 10.5,
}, {
'prefix': '12345',
'price': 22.5,
}];
const apiRequest = {
value: '123456789',
// or even
// value: 123456789,
};
const apiResponse = serverSideData
.reduce(collectItemOfBestCoveringEntryValue, {
search: String(apiRequest.value),
result: null,
}).result;
console.log({ apiResponse });
.as-console-wrapper { min-height: 100%!important; top: 0; }