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);