Ad

Sort Array Of Objects Depending On Another Array Values Javascript

- 1 answer

I have two arrays: First array :

SelectedRows= 
[
   {
      "NR":"4",
      "KUNNR":"9?AMT132",
      "NAME":" AUTO TOURING HANDELS GES.M.B.H.",
      "LATLON":[
         15.4114217,
         47.0664085
      ],
      "LIN":"LIN097"
   },
   {
      "NR":"3",
      "KUNNR":"9Z?CH005",
      "NAME":" Z?CHNER ERDBAU U TRANSPORT GMBH",
      "LATLON":[
         13.4216536,
         47.3747925
      ],
      "LIN":"LIN099"
   },
   {
      "NR":"2",
      "KUNNR":"9SMTA001",
      "NAME":" SMT AUTOTEILE",
      "LATLON":[
         13.2277803,
         47.9525892
      ],
      "LIN":"LIN0102"
   },
   {
      "NR":"1",
      "KUNNR":"9REIT051",
      "NAME":" W.REITINGER GMBH",
      "LATLON":[
         14.4017044,
         48.2213305
      ],
      "LIN":"LIN0103"
   }
]

Second array is

LOCATIONS =
[
   [
      15.4114217,
      47.0664085
   ],
   [
      14.4017044,
      48.2213305
   ],
   [
      13.2277803,
      47.9525892
   ],
   [
      13.4216536,
      47.3747925
   ]
]

I need to sort the first array (SelectedRows) on matching values from SelectedRows.LATLON on the second array(LOCATIONS)

I tried to do:

var Output= LOCATIONS.filter(function(obj) { 
            return SelectedRows.LATLON.indexOf(obj) == -1; 
        });

but it did not work. SO if anyone can give me an advice I will be so gratefull. Sorry for my bad english, to get the idea I am posting the expected output:

Expected output :

Output=[
{
    "NR":"4",
    "KUNNR":"9?AMT132",
    "NAME":" AUTO TOURING HANDELS GES.M.B.H.",
    "LATLON":[
        15.4114217,
        47.0664085
    ],
    "LIN":"LIN097"
},
{
    "NR":"1",
    "KUNNR":"9REIT051",
    "NAME":" W.REITINGER GMBH",
    "LATLON":[
        14.4017044,
        48.2213305
    ],
    "LIN":"LIN0103"
},
{
    "NR":"2",
    "KUNNR":"9SMTA001",
    "NAME":" SMT AUTOTEILE",
    "LATLON":[
        13.2277803,
        47.9525892
    ],
    "LIN":"LIN0102"
},
{
    "NR":"3",
    "KUNNR":"9Z?CH005",
    "NAME":" Z?CHNER ERDBAU U TRANSPORT GMBH",
    "LATLON":[
        13.4216536,
        47.3747925
    ],
    "LIN":"LIN099"
}]

Thanks for your help

Ad

Answer

what you should be doing is to mapLOCATIONS

in the map you find the appropriate item in SelectedRows ...

be aware that [1,2] !== [1,2] so you can't compare arrays in your find, since you'll never find anything - either compare each element of the arrays, or, in this case, you could check if array1.join() === array2.join()

const SelectedRows = [ { "NR":"4", "KUNNR":"9?AMT132", "NAME":" AUTO TOURING HANDELS GES.M.B.H.", "LATLON":[ 15.4114217, 47.0664085 ], "LIN":"LIN097" }, { "NR":"3", "KUNNR":"9Z?CH005", "NAME":" Z?CHNER ERDBAU U TRANSPORT GMBH", "LATLON":[ 13.4216536, 47.3747925 ], "LIN":"LIN099" }, { "NR":"2", "KUNNR":"9SMTA001", "NAME":" SMT AUTOTEILE", "LATLON":[ 13.2277803, 47.9525892 ], "LIN":"LIN0102" }, { "NR":"1", "KUNNR":"9REIT051", "NAME":" W.REITINGER GMBH", "LATLON":[ 14.4017044, 48.2213305 ], "LIN":"LIN0103" } ], LOCATIONS = [ [ 15.4114217, 47.0664085 ], [ 14.4017044, 48.2213305 ], [ 13.2277803, 47.9525892 ], [ 13.4216536, 47.3747925 ] ] 

const output =  LOCATIONS.map(
  a=>SelectedRows.find(({LATLON}) => LATLON.join() === a.join())
);

console.log(output);
.as-console-wrapper {max-height: 100%!important; top:0; }
.as-console-row::after { display:none !important; }

Although, for truly readable code

const SelectedRows = [ { "NR":"4", "KUNNR":"9?AMT132", "NAME":" AUTO TOURING HANDELS GES.M.B.H.", "LATLON":[ 15.4114217, 47.0664085 ], "LIN":"LIN097" }, { "NR":"3", "KUNNR":"9Z?CH005", "NAME":" Z?CHNER ERDBAU U TRANSPORT GMBH", "LATLON":[ 13.4216536, 47.3747925 ], "LIN":"LIN099" }, { "NR":"2", "KUNNR":"9SMTA001", "NAME":" SMT AUTOTEILE", "LATLON":[ 13.2277803, 47.9525892 ], "LIN":"LIN0102" }, { "NR":"1", "KUNNR":"9REIT051", "NAME":" W.REITINGER GMBH", "LATLON":[ 14.4017044, 48.2213305 ], "LIN":"LIN0103" } ], LOCATIONS = [ [ 15.4114217, 47.0664085 ], [ 14.4017044, 48.2213305 ], [ 13.2277803, 47.9525892 ], [ 13.4216536, 47.3747925 ] ] 

const output = LOCATIONS.map(([tgtLat, tgtLon]) =>
  SelectedRows.find(
    ({ LATLON: [lat, lon] }) => tgtLat === lat && tgtLon === lon
  )
);

console.log(output);
.as-console-wrapper {max-height: 100%!important; top:0; }
.as-console-row::after { display:none !important; }

Ad
source: stackoverflow.com
Ad