Fill an array with unique object property value

I build an array of random objects
but how can I do to get it the same length and
with unique property value? example:

var allR= [{"id": 1,"value": "Antelope"},{"id": 2,"value": "Bisont"},{"id": 3,"value": "Crocodile"},{"id": 4,"value":"Nasica"},{"id": 5,"value":"Porcupine" },{"id": 6,"value":"Llama" },{"id": 7,"value":"Orangutan"},{"id": 8,"value":"Elephant"},{"id": 8,"value":"Piton"},{"id": 9,"value":"Quetzal"},{"id": 10,"value":"Rinho"},{"id": 11,"value":"Gnu"}];

function getRandomInt(min, max) {
 // return Math.floor(Math.random() * (max - min)) + min;
//  return Math.floor(Math.random() * (max - min)) + min;
  return allR[Math.floor(Math.random()*allR.length)];
}
function getRandomInts(num) {
  var ints = [];
  while (ints.length < num-1) {
    var randNum = getRandomInt(1, 91);
    if(!ints.indexOf(randNum) > -1){ // need reflect to object property and not indexOf?
      ints.push(randNum);
    }
  }
  return ints;
}
var arr = getRandomInts(5);
console.log(arr);

(4) […]
0: Object { id: 8, value: "Elephant" }
1: Object { id: 3, value: "Crocodile" }
2: Object { id: 5, value: "Porcupine" }
3: Object { id: 5, value: "Porcupine" } // here i need no douples
length: 4

thank a lot

Can you give an example of how you would like to call the function to generate the array of objects, as well as an example of the output you would like (I assume you don’t want animals).

Scenario is fill premade 2D matrix array. So for example width = 20, height = 4

                function createGround(width, height) {
                    var result = [];

                    for (var i = 0; i < width; i++) {
                        result[i] = [];
                        for (var j = 0; j < height; j++) {
                                 
                            if (tacArray.length / 2 <= width) {
                                //do something;
               
                            } else {

       
                               result[i][j] = **Array with 4 unique and randomizate Objects**

                            }
                           
                        }
                    }
                    return result;
                }

                var ground = createGround(20, 4);

ps animals array in reality are peoples arry but property is tha same…

So how would you like to call createGround and what should the output look like?

So where are the people coming from? Do you have an array that you want to grab random values from (excluding duplicates)?

ok, people come from an ajax request from db. The ultimate goal are create work shift for assist seek animals by volunteeer. So imagine that i heve 10 volunteers and for a determinate animal need 4 volunteers at day, i need to to distribute 4 for day for 20 days (mounth). Matrix array width are days(20) and height are peole to stay on service…
Problem to solve have 4 different peoples at day

Sorry, I don’t understand this ^.

One last time: how would you like to call the function and what should the output look like?

  • how would you like to call the function
    from a for loop for n time. If you look at first sntence notice this:
for (var j = 0; j < height; j++) { 
                            if (tacArray.length / 2 <= width) {
                                //do something;               
                            } else {      
                               result[i][j] = **Here... i want to call and retrieve array with 4 objects**
                            }
  • what should the output look like?

as follow


(4) […]
0: Object { id: 6, value: "Llama" }
1: Object { id: 9, value: "Quetzal" }
2: Object { id: 1, value: "Antelope" }
3: Object { id: 3, value: "Crocodile" }
length: 4
​
0: Object { id: 10, value: "Rinho" }
1: Object { id: 4, value: "Nasica" }
2: Object { id: 2, value: "Bisont" }
3: Object { id: 1, value: "Antelope" }
length: 4
​
(4) […]
0: Object { id: 8, value: "Piton" }
1: Object { id: 11, value: "Gnu" }
2: Object { id: 2, value: "Bisont" }
3: Object { id: 9, value: "Quetzal" }
length: 4

(4) […]
0: Object { id: 11, value: "Gnu" }
1: Object { id: 6, value: "Llama" }
2: Object { id: 1, value: "Antelope" }
3: Object { id: 6, value: "Llama" }
length: 4

for 20 time array with no duplicate object (this example 4…)

hope more cleared

I’m still not following 100%.

So you have an array of objects populated by an Ajax call:

const arr = [
  { id: 1, value: "Antelope" },
  { id: 2, value: "Bisont" },
  { id: 3, value: "Crocodile" },
  ...
  { id: 10, value: "Rinho" }
];

And now you want to create a function to pluck objects out of that array, avoiding duplicates. E.g.

const arr = [  ... ];

getAnimals(3);
// returns 3 random animals from the array avoiding duplicates:
// [
//   { id: 9, value: "Quetzal" },
//   { id: 2, value: "Bisont" },
//   { id: 4, value: "Nasica" }
// ]

Or:

const arr = [  ... ];

getAnimals(4);
// returns 4 random animals from the array avoiding duplicates:
// [
//   { id: 1, value: "Antelope" },
//   { id: 4, value: "Nasica" },
//   { id: 10, value: "Rinho" },
//   { id: 8, value: "Piton" }
// ]

Did I get that right?

Yes!

1 Like

Like this?

const animalsArray = [
  { id: 1, value: 'Antelope' },
  { id: 2, value: 'Bisont' },
  { id: 3, value: 'Crocodile' },
  { id: 4, value: 'Nasica' },
  { id: 5, value: 'Gnu' },
  { id: 6, value: 'Llama' },
  { id: 7, value: 'Cat' },
  { id: 8, value: 'Piton' },
  { id: 9, value: 'Quetzal' },
  { id: 10, value: 'Rinho' }
];

function getAnimals(arr, num) {
  const shuffled = arr.sort(() => 0.5 - Math.random());
  return shuffled.slice(0, num);
}

console.log(getAnimals(animalsArray, 4));

// (4) […]
// 0: Object { id: 3, value: "Crocodile" }
// ​1: Object { id: 4, value: "Nasica" }
// ​2: Object { id: 7, value: "Cat" }
// ​3: Object { id: 1, value: "Antelope" }
3 Likes

EXACTLY what i want!!! very elegant solution with very few lines of code …

I don’t know how to thank you enough

1 Like

No worries. Happy to help :slight_smile:

2 Likes