Skip to content
Advertisement

How can one access the related item from a 2nd array if one just has the randomly chosen item of the first array?

Basically, I have two arrays of related array items, where one item is/got randomly chosen.

I want to print the related item of the 2nd array. Let’s say one has an array of job names, and it looks like this:

const jobNames = ["Builder", "Doctor", "Vet"];

Then one picks a random job name like that:

var randomJobName = jobNames[ Math.floor(Math.random() * jobNames.length) ];

Now one has another array which, with each item, features the description of its related job name.

How can one access the related item from the 2nd array if one just has the randomly chosen item of the first array?

Advertisement

Answer

There are basically 3 different approaches.

The easiest one is, as already suggested by VLAZ, to get a random index once and access both items each from its array by this random index.

const jobNames = ['Builder', 'Doctor', 'Vet'];
const jobDescriptions = [
  'creates/builds things/stuff.',
  'tries fixing human health issues.',
  'does fix animal health issues.',
];
const randomIdx = Math.floor(Math.random() * jobNames.length);

const jobName = jobNames[randomIdx];
const jobDescription = jobDescriptions[randomIdx];

console.log({ jobName, jobDescription });
.as-console-wrapper { min-height: 100%!important; top: 0; }

In case the OP has not the chance of using a single random index at time, and thus is forced to work with the already randomly picked job name, then the OP needs to find the index of this very job name from where the OP then can proceed with picking the related job description …

const jobNames = ['Builder', 'Doctor', 'Vet'];
const jobDescriptions = [
  'creates/builds things/stuff.',
  'tries fixing human health issues.',
  'does fix animal health issues.',
];
// OP has no control about how `jobName` is/was/gets chosen.
const jobName = jobNames[ Math.floor(Math.random() * jobNames.length) ];

const index = jobNames.indexOf(jobName);
const jobDescription = jobDescriptions[index];

console.log({ jobName, jobDescription });
.as-console-wrapper { min-height: 100%!important; top: 0; }

In case such a task has to be run often/repeatedly and in case the OP can change code as the OP is pleased an approach was to combine both arrays into an array of aggregated job items (name and description) and make the random pick once from this new array structure …

const jobNames = ['Builder', 'Doctor', 'Vet'];
const jobDescriptions = [
  'creates/builds things/stuff.',
  'tries fixing human health issues.',
  'does fix animal health issues.',
];
const jobList = jobNames
  .map(function (name, idx) {

    const description = this[idx];
    return {
      name,
      description,
    };
                        // `jobDescriptions` applied as
  }, jobDescriptions);  // `map`'s 2nd `thisArg` argument.

function getRandomArrayIndex(arr) {
  return Math.floor(Math.random() * arr.length);
}

const {
  name,
  description,
} = jobList[ getRandomArrayIndex(jobList) ];

console.log({ name, description });
console.log({ jobList });
.as-console-wrapper { min-height: 100%!important; top: 0; }
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement