0

I have multiple arrays like

 array1 = ["car","boot","bike"];
 array2 = ["table","human","cat"];
 array3 = ["boot","pc","iphone"];
 array4 = ["boot","pc","iphone"];
 array5 = ["bike","human","pet"];

and this is the code to get randomly a array

  var card;
  var rand;
  var arr = [1,2,3,4,5];
  rand = arr[Math.floor(Math.random() * arr.length)];

 if(rand == 1){ card = array1; }else 
 if(rand == 2){ card = array2; }else 
 if(rand == 3){ card = array3; }else 
 if(rand == 4){ card = array4; }else 
 if(rand == 5){ card = array5; }

How can I select from only the arrays without a value "bike" or how can I select from only the arrays without "bike" in the 3rd place in a array?

6
  • try if(!array.include('bike')) which will eliminate the bike Commented Mar 14, 2017 at 17:03
  • else try array.splice('bike',1) Commented Mar 14, 2017 at 17:05
  • So all the ifs are 1? You probably should use a nested array so you can loop over them easier. Commented Mar 14, 2017 at 17:06
  • Might as well save yourself a bunch of code and some headaches and place all the arrays into an array as well. Then you can use .filter(). Commented Mar 14, 2017 at 17:08
  • the ifs are not all 1 Commented Mar 14, 2017 at 17:09

5 Answers 5

1
// We want one array containing all the data, so we only have to look at one place.
// Remember, an array can contain anything, including other arrays.
var words = [
    ["car","boot","bike"],
    ["table","human","cat"],
    ["boot","pc","iphone"],
    ["boot","pc","iphone"],
    ["bike","human","pet"]
];

// Let's make a function we can reuse to make things easier
var getArray = function( wordToIgnore ) {
    // we need a random number here, but we also want to filter out any arrays that contain the word we have to ignore.
    // SO we do the filter first, since if affects the random number.
    var arraysToSearch = words.filter(function ( array ) {
        // Standard filter function. We have to return true if the array should stay or false if it has to be removed.
        // So we look at the index of the word we're searching for.
        // If the word exists, we want to remove the array, else keep it.
        return array.indexOf( wordToIgnore ) === -1;

        // To get the 'bike' in 3rd position filter, we just need to update the filter function so it only looks at the 3rd position instead of any index.
        // return array[2] === wordToIgnore;
    });
    // Now that we have the arrays we want to search in, we need a random array of those remaining arrays.
    // If we multiply Math.random() with a number, we directly get the number we need.
    // So with 3 arrays remaining, we want an index between 0 and 2
    var index = Math.floor( Math.random() * (arraysToSearch.length) );
    // Now that we have an index and an array filled with valid arrays, we can just return one;
    return arraysToSearch[ index ];
};

// Let's use our function!
var randomArrayWithoutABike = getArray('bike');
Sign up to request clarification or add additional context in comments.

Comments

0

You can remove the bike value from the generated array as following

card = array1.splice(array1.indexOf('bike'),1);

I hope this bit of code will remove the bike from the array and give you the output element without bike.

2 Comments

This will permanently change the array. You probably mean slice() instead.
he can make a copy of the array in some function, but this method will work
0

Using the filter method on array.

var arr = [];
array1 = ["car","boot","bike"];
 array2 = ["table","human","cat"];
 array3 = ["boot","pc","iphone"];
 array4 = ["boot","pc","iphone"];
 array5 = ["bike","human","pet"];
 
 arr.push(array1);
 arr.push(array2);
 arr.push(array3);
 arr.push(array4);
 
 console.log(arr.filter(x => x[2] !== "bike"));

Comments

0

Whenever you're naming variables something1, something2, something3 it's usually that you should be using an array. Your current logic, ironically, already works with an array to pick a random element, so you don't need to make these elements their own variables.

var items = [
  ["car", "boot", "bike"],
  ["table","human","cat"],
  ["boot","pc","iphone"],
  ["boot","pc","iphone"],
  ["bike","human","pet"]
];

var card = items[Math.floor(Math.random() * items.length)];

Now to pick only certain elements from an array, you can use the .filter method.

var nobike = items.filter(function (val, i, arr) {
  return val[2] !== 'bike';
});

The filter method takes a callback function and loops through the array. The callback must return true if the element must be kept and false if it must be discarded. Here, we told it to keep subarrays that don't have 'bike' in their third cell. So nobike contains all but the first array.

As to why you're trying to do any of this and why you have an array of arrays of three cells, I have no clue.

4 Comments

Just what i am looking for. Thanks. But how can you return 3 values like val[0] val[1] val[2[ val[0] !== bike val[1[ !== pc val[2[ !== pet
The filter function decides to keep (or not) each cell of an array. In our case, this array contains values that are arrays. If you want, you can put multiple conditions, but I'm not sure what you're trying to accomplish at this point.
what i meen is. how can i filter all the arrays with pet in slot 1 and pc in slot2 and cat in slot 3.
If you want to filter out any array that has at least one of those criteria, you'd make your filter return val[0] !== 'pet' && val[1] !== 'pc' && val[2] !== 'cat'
0

You can make an array containing your variables array1, array2, array3, etc. and select them randomly using the Array#filter(predicate) function.

function random (array) {
  return array[Math.floor(Math.random() * array.length)]
}

function filteredRandom (array, predicate) {
  return random(array.filter(predicate))
}

var arrays = [
  ["car", "boot", "bike"],
  ["table", "human", "cat"],
  ["boot", "pc", "iphone"],
  ["boot", "pc", "iphone"],
  ["bike", "human", "pet"]
]


// Regular:
console.log(random(arrays))

// Filtering out arrays containing 'bike':
console.log(
  filteredRandom(arrays, function (e) {
    return e.indexOf('bike') < 0
  })
)

// Filtering out arrays with 'bike' in the 3rd slot:
console.log(
  filteredRandom(arrays, function (e) {
    return e[2] !== 'bike'
  })
)
.as-console-wrapper { min-height: 100vh; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.