What am I doing?
So I am creating an array of objects from data found in a sheet. The sheet data looks something like this:
Name Lat Long
test1 -3.4545 1.2568
test2 -5.4898 14.4589
test3 -3.4545 1.2568
What do I want to do?
I have to add a field to each object saying if they belong to campus or not, and in order to do that, I have to check if the object has the same lat and long as another object. Example of what I want to do:
[
{
name: "test1",
campus: "Yes" --> because the lat and long is the same in object test3,
lat: -3.4545,
long: 1.2568
},
{
name: "test2",
campus: "No",
lat: -5.4898,
long: 14.4589
},
{
name: "test3",
campus: "Yes" --> because the lat and long is the same object test1,
lat: -3.4545,
long: 1.2568
}
]
What have I done so far?
I am iterating over the sheet and creating the objects:
let worksheets1 = {}
for (const sheetName1 of workbook.SheetNames) {
worksheets1[sheetName1] = XLSX.utils.sheet_to_json(workbook.Sheets['Sheet1'], { header: 1 })
}
let data = worksheets1['Sheet1']
for (const rows of data) {
if(rows[10] == "New"){
aux2.push({
name:rows[10],
campus: "", //I need this
lat: rows[35],
long: rows[36],
code: rows[7] !== undefined ? rows[7] : "No info",
info:rows[17] !== undefined
? {
data: rows[17].replace(/,\s/g, ",").split(","),
source: "Sheet"
}
: "No info"
})
}
}
aux2.shift()
In summary
I wonder if there is a way to compare on the go if two or more objects have the same lat and long, and if they do have, I already add it to the field campus "Yes" or "No"?
Thanks.