Currently, my object looks like this:
obj1 = {a:'1', b:'2', c:'3', d:'4'}
What I am trying to get it to look like is this:
resultsArray: [{a: '1', b: '2'}, {c: '3', d: '4'}]
I've used Object.entries(obj1) which returns:
[ ['a', '1'], ['b', '2'], ['c', '3'], ['d', '4'] ]
I'm just not sure how to combine them. Would I just iterate through it like a normal array and concact to a new array with the pairs?
const express = require("express");
const app = express();
const axios = require("axios");
const cheerio = require("cheerio");
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static("public"));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname + "./public/index.html"));
});
axios
.get("https://www.espn.com/mlb/scoreboard/_/date/20220407")
.then(function (res) {
let $ = cheerio.load(res.data);
let teams = [];
let teamList = $(".ScoreCell__Truncate").each(function (i, elem) {
teams.push($(elem).text());
});
let scores = [];
let gameScores = $(".ScoreboardScoreCell__Value").each(function (i, elem) {
scores.push($(elem).text());
});
let onlyRuns = adjustRuns(scores);
const gameResults = {};
teams.forEach((element, index) => {
if (onlyRuns[index] !== undefined) {
gameResults[element] = parseInt(onlyRuns[index]);
}
});
});
app.listen(3000, function () {
console.log("App is listening on port 3000");
});
function adjustRuns(arr) {
const resultsArr = [];
for (let i = 0; i < arr.length; i += 3) {
resultsArr.push(arr[i]);
}
return resultsArr;
}