Wonder if someone can help me traverse this array so that the cardNumber can be included in the deviceListJson.
So at the moment I have two json files which I read off.
const deviceListData = await readJSON(deviceFilePath);
const playerListData = await readJSON(playersFilePath);
deviceFilePath contains all the devices like so:
{
"deviceId": "9331d5fwwd77",
"slot": 1,
"deviceType": "GAME"
},
{
"deviceId": "9221d5fwwd77",
"slot": 3,
"deviceType": "GAME"
},
{
"deviceId": "9001d5fwwd77",
"slot": 10,
"deviceType": "GAME"
},
{
"deviceId": "9331d5fww007",
"slot": 11,
"deviceType": "GAME"
},
{
"deviceId": "9331d5f00077",
"slot": 19,
"deviceType": "GAME"
}
PlayerListData contains all the player info json like so:
{
"cardNumber": "199",
"firstName": "TIM",
"middleName": "P",
},
{
"cardNumber": "110",
"firstName": "JACK",
"middleName": "Z",
},
{
"cardNumber": "111",
"firstName": "MICKEY",
"middleName": "A",
}
What I want to do is simply add a random card number from the playerListData Json into the deviceListData Json.
So for example deviceListData json will look like this:
{
"deviceId": "9331d5fwwd77",
"slot": 1,
"deviceType": "GAME",
"cardNumber": "111"
},
{
"deviceId": "9221d5fwwd77",
"slot": 3,
"deviceType": "GAME",
"cardNumber": "110"
},
{
"deviceId": "9001d5fwwd77",
"slot": 10,
"deviceType": "GAME",
"cardNumber": "199"
},
{
"deviceId": "9331d5fww007",
"slot": 11,
"deviceType": "GAME",
"cardNumber": "199"
},
{
"deviceId": "9331d5f00077",
"slot": 19,
"deviceType": "GAME",
"cardNumber": "111"
}
So I do a map like so:
const cardNumber = playerListData.map((item) => item.cardNumber);
console.log(cardNumber);
And it outputs this array (I notice it doesn’t include the key, just the values in the array of the card numbers)
[ '199', '110', '111' ]
When I piece together the whole code, what happens is that it adds all of the fields from the playerListJson into DeviceListJson like so:
{
"deviceId": "9331d5fwwd77",
"slot": 1,
"deviceType": "GAME",
"cardNumber": "111",
"firstName": "MICKEY",
"middleName": "A"
},
{
"deviceId": "9221d5fwwd77",
"slot": 3,
"deviceType": "GAME",
"cardNumber": "110",
"firstName": "JACK",
"middleName": "Z"
},
{
"deviceId": "9001d5fwwd77",
"slot": 10,
"deviceType": "GAME",
"cardNumber": "199",
"firstName": "TIM",
"middleName": "P"
},
{
"deviceId": "9331d5fww007",
"slot": 11,
"deviceType": "GAME",
"cardNumber": "199",
"firstName": "TIM",
"middleName": "P"
},
{
"deviceId": "9331d5f00077",
"slot": 19,
"deviceType": "GAME",
"cardNumber": "111",
"firstName": "MICKEY",
"middleName": "A"
}
This is the code:
export async function assignPlayerCards() {
try {
const deviceListData = await readJSON(deviceFilePath);
const playerListData = await readJSON(playersFilePath);
const cardNumber = playerListData.map((item) => item.cardNumber);
// Function to get a random card number from player list json
function getRandomJson1Element() {
const randomIndex = Math.floor(Math.random() * cardNumber.length);
// console.log(cardNumber[randomIndex]);
return playerListData[randomIndex];
}
// Update each element in device JSON with a random card number from player list JSON
deviceListData.forEach((element) => {
const randomElement = getRandomJson1Element();
Object.assign(element, randomElement);
});
await writeJSON(deviceFilePath, deviceListData);
console.log("Headers set for devices");
} catch (error) {
throw error;
}
}
What am I doing wrong and how can I get it to meet my requirement which is to just add the cardNumber into the deviceListJson?
2
Answers
Right now you’re adding all fields with
Object.assign
, what you want to do is to only add one field, so you could replace yourforEach
loop:with the following:
Select random index from array
playerListData
And with mapping
deviceListData
array and over each one and withdestructing
build another object withcardNumber
property selected from randomly player.