-2
let courts = [
    // dynamic
    { courtID: 1, court: "Onzie" },
    { courtID: 2, court: "Twozie" },
    { courtID: 3, court: "Threezie" },
    { courtID: 4, court: "Fourzie" }
];

let players = [
    // dynamic
    { player: "Bengt", id: 1, court: null },
    { player: "Robin", id: 2, court: null },
    { player: "Phil", id: 3, court: null },
    { player: "Pontus", id: 4, court: null }
];

How can i combine these two arrays so that the player(s) are randomly matched with a corresponding court?

Tenkaklet
  • 1
  • 3

1 Answers1

-1

Live demo https://runkit.com/embed/e67flbg3miew

enter image description here

Update: According to the changes of the OP, use below updated snippet:

const rand = max =>  Math.floor(Math.random() * Math.floor(max));
let one = courts [rand(courts.length -1)];
let two = players.find(item => 
              courts.map(entry => entry.court).includes(item.court));
let output = Object.assign(Object.assign({}, one), two);                    

Original:

const rand = max =>  Math.floor(Math.random() * Math.floor(max));

let one = courts [rand(courts.length -1)];
let two = players [rand(players.length -1)];

let output = Object.assign ( Object.assign({}, one), two);
David
  • 15,894
  • 22
  • 55
  • 66