[
{
"id": 1,
"word": "slowly",
"pos": "adverb"
},
{
"id": 2,
"word": "ride",
"pos": "verb"
},
{
"id": 3,
"word": "bus",
"pos": "noun"
},
{
"id": 4,
"word": "commute",
"pos": "verb"
},
{
"id": 5,
"word": "emissions",
"pos": "noun"
},
{
"id": 6,
"word": "walk",
"pos": "verb"
},
{
"id": 7,
"word": "fast",
"pos": "adjective"
},
{
"id": 8,
"word": "car",
"pos": "noun"
},
{
"id": 9,
"word": "crowded",
"pos": "adjective"
},
{
"id": 10,
"word": "arrival",
"pos": "noun"
},
{
"id": 11,
"word": "emit",
"pos": "verb"
},
{
"id": 12,
"word": "independent",
"pos": "adjective"
},
{
"id": 13,
"word": "convenient",
"pos": "adjective"
},
{
"id": 14,
"word": "lane",
"pos": "noun"
},
{
"id": 15,
"word": "heavily",
"pos": "adverb"
}
]
Return Random Array With 10 Items In JavaScript, The array should include at least 1 of each "pos"
want to return a random array that includes 10 items from the array above, which should have at least 1 adjective, 1 adverb, 1 noun, and 1 verb.
I tried to do it but I'm getting random data cannot control having at least 1 adjective, 1 adverb, 1 noun, and 1 verb.
CodePudding user response:
Make an array each for the parts of speech.
const allPos = p => bigArray.filter(word => word.pos === p);
const verbs = allPos("verb");
const nouns = //...
Borrow a shuffler function (like this)
shuffle(verbs);
shuffle(nouns);
//...
Build the result with a round-robin pop from each array...
let posGroups = [verbs, nouns, ...];
let posIndex = 0;
let count = 0
let result = [];
while (count < 10) {
let word = posGroups[posIndex].pop();
if (word) result.push(word);
posIndex = posIndex < posGroups.length-1 ? posIndex 1 : 0;
}
CodePudding user response:
Since you seem pretty new to this subject, here is a more lengthy way with comments to explain how to solve your problem. This is possible in a more concise way (see answer of danh), but I think that a step-by-step solution will be more readable for a beginner.
Note: This solution does not implement a shuffle algorithm. If you care about that, see answer of danh.
// Your array containing the mentioned objects.
const items = [
{
id: 1,
word: 'slowly',
pos: 'adverb',
},
{
id: 2,
word: 'ride',
pos: 'verb',
},
{
id: 3,
word: 'bus',
pos: 'noun',
},
{
id: 4,
word: 'commute',
pos: 'verb',
},
{
id: 5,
word: 'emissions',
pos: 'noun',
},
{
id: 6,
word: 'walk',
pos: 'verb',
},
{
id: 7,
word: 'fast',
pos: 'adjective',
},
{
id: 8,
word: 'car',
pos: 'noun',
},
{
id: 9,
word: 'crowded',
pos: 'adjective',
},
{
id: 10,
word: 'arrival',
pos: 'noun',
},
{
id: 11,
word: 'emit',
pos: 'verb',
},
{
id: 12,
word: 'independent',
pos: 'adjective',
},
{
id: 13,
word: 'convenient',
pos: 'adjective',
},
{
id: 14,
word: 'lane',
pos: 'noun',
},
{
id: 15,
word: 'heavily',
pos: 'adverb',
},
];
// The function for selecting random elements from the array.
function getRandomItems(itemCount) {
// The array we will use for our selected items.
const selectedItems = [];
// Get all unique values for property "pos".
const distinctPos = [...new Set(items.map((item) => item.pos))];
// Subtract the count of found distinct "pos" values from the item count.
itemCount -= distinctPos.length;
if (itemCount < 0) {
// Implement proper error handling.
throw "The count of items cannot be less than the amount of distinct pos!";
}
// Iterate through every found distinct "pos" value.
for (i = 0; i < distinctPos.length; i ) {
// Get all objects for the current distinct value of "pos".
const itemsWithDistinctPos = items.filter(
(item) => item.pos === distinctPos[i]
);
// Get a random item with the current "pos" value and add it to the result array.
selectedItems.push(getRandomItemFromArray(itemsWithDistinctPos));
}
// Get the rest of the values without caring about the "pos" value.
for (i = 0; i < itemCount; i ) {
// Add a random object from the array to the result array.
selectedItems.push(getRandomItemFromArray(items));
}
// Return the result array.
return selectedItems;
}
function getRandomItemFromArray(array) {
// Get a random element from the array.
return array[Math.floor(Math.random() * array.length)];
}
// Now call the method like this.
const selectedItems = getRandomItems(10);