'How to compare one or more fields while iterating in javascript?

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.



Solution 1:[1]

Sort your array by the lat/long properties of the contained objects. Any objects having the same values will then be adjacent array elements so you can assign the campus flag in a single pass.

Sample code:

let orig = [
     {
       name: "test1",
       lat: -3.4545,
       long: 1.2568 
     },
     {
       name: "test2",
       lat: -5.4898,
       long: 14.4589 
     },
     {
       name: "test3",
       lat: -3.4545,
       long: 1.2568
     } 
    ];
    
orig
    .sort (
        (a,b) => {
            let c =
                (a.lat < b.lat)
                    ? -1
                    : ((a.lat > b.lat)
                            ? 1
                            : ((a.long < b.long) ? -1 : ((a.long > b.long) ? 1 : 0 ))
                      )
              ;
            
            return c;
        });
for (let i=1; i < orig.length; i++) {
    orig[i-1].campus = ((orig[i-1].lat === orig[i].lat) && (orig[i-1].long === orig[i].long)) ? 'Yes' : 'No';
}
if (orig.length > 1) {
    orig[orig.length-1].campus = ((orig[orig.length-2].lat === orig[orig.length-1].lat) && (orig[orig.length-2].long === orig[orig.length-1].long)) ? 'Yes' : 'No';
}
orig;

The custom comparison function can be enhanced, of course, to actually test whether coordinates are within some prespecified campus parameter. The specific return value if two objects differ in that respect is not important as long as you return 0 for all pairs (and only for such pairs) that are deemed 'equal'.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1