I want to rearrange the sentences so that each sentence is a prerequisites for the next sentence and here is what I have and it works fine:
const sentences = [
"he has eaten a pizza at the party last night",
"he has eaten a pizza",
"he has eaten a pizza at the party",
]
function sessionCardsSort(arr) {
arr = arr.map(part => part.split(' ')).sort((a, b) => a.length - b.length);
function sortBy(list, main) {
return list.sort((a, b) => {
let count1 = 0, count2 = 0;
a.forEach(part => { if(main.includes(part)) { count1++ } });
b.forEach(part => { if(main.includes(part)) { count2++ } });
return count1 - count2;
});
}
return sortBy(arr, arr[arr.length - 1]).map(part => part.join(' '));
}
console.log(sessionCardsSort(sentences));
As you see I used a simple array named sentences
as data structure to work with, but what if we want to use the same data of sentences
array with new structure like this:
const sentences = [
{ reference: ['he has eaten a pizza at the party', 'last night'] }
{ reference: ['"he has', 'eaten a pizza'] },
{ reference: ['he', 'has', 'eaten a pizza at the party'] },
]
So the issue is I want to implement the exact same function this time for array of objects not just a simple array!
Here is desired result (only using the exact above function):
const sentences = [
{ reference: ['"he has', 'eaten a pizza'] },
{ reference: ['he', 'has', 'eaten a pizza at the party'] },
{ reference: ['he has eaten a pizza at the party', 'last night'] }
]
Note: In the second data structure joining elements inside each reference
will give you the same sentence as the first example, for instance :
{ reference: ['he has eaten a pizza at the party', 'last night'] }
"he has eaten a pizza at the party last night"
Note: Please note that I want to implement the exact same function