我有兩個陣列: 第一個陣列:
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
]
]
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
uj5u.com熱心網友回復:
你應該做的是映射 LOCATIONS
在地圖中,您可以在...中找到適當的專案SelectedRows
請注意,[1,2] !== [1,2]因此您無法比較查找中的陣列,因為您將永遠找不到任何東西 - 或者比較陣列的每個元素,或者在這種情況下,您可以檢查是否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; }
雖然,對于真正可讀的代碼
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; }
uj5u.com熱心網友回復:
以下代碼有點臟,但它有效:
LOCATIONS_ORDER = LOCATIONS.map(e => `${e[0]};${e[1]}`);
SelectedRows.forEach(e => e.index = LOCATIONS_ORDER.indexOf(`${e.LATLON[0]};${e.LATLON[1]}`));
SelectedRows.sort((a,b) => a.index-b.index);
console.log(SelectedRows);
uj5u.com熱心網友回復:
O(n)鑒于您n同時擁有以下元素,selectedRows因此最快的方式(至少漸近地)及時執行此操作locations。
- 回圈遍歷正確順序的位置,并將位置值映射到它們(應該是)所在的索引。您不能為此使用陣列,因為無法使用
Object.is()which 在Map內部使用陣列來比較陣列。因此,例如,您可以在此處使用連接字串。可能有更好/更清潔的選擇,但這很簡單并且可以完成作業。這將采取O(n).
然后地圖將如下所示:
Map(4) {
'15.4114217,47.0664085' => 0,
'14.4017044,48.2213305' => 1,
'13.2277803,47.9525892' => 2,
'13.4216536,47.3747925' => 3
}
- 回圈遍歷
selectedRows每個物件的 and 確定它的鍵,這又是連接的位置,并使用該鍵獲取物件應該位于的索引O(1)。如果當前索引和目標索引匹配一切都很好。如果沒有,我們需要將物件交換到目標索引,這顯然是在O(1). 這將總共也采取O(n)。
完畢。合并這些步驟需要O(n)時間,因此它將是一個漸近最優演算法。
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"
}
]
const locations =
[
[
15.4114217,
47.0664085
],
[
14.4017044,
48.2213305
],
[
13.2277803,
47.9525892
],
[
13.4216536,
47.3747925
]
]
console.log("Previous", selectedRows);
const indexMap = new Map();
// Map values to indices
locations.forEach((loc, idx) => indexMap.set(`${loc[0]},${loc[1]}`, idx));
// put each object to the correct position
selectedRows.forEach((row , i) => {
// concatenate lookup key
const key = `${row.LATLON[0]},${row.LATLON[1]}`;
// check if value is actually in map => should always be the case (assumption: locations and showLocation contain the same locations)
if(indexMap.has(key)){
const targetIndex = indexMap.get(key);
// check if the object is at the right position
if(!targetIndex === i){
// position is wrong => swap positions
const temp = selectedRows[i];
selectedRows[i] = selectedRows[targetIndex];
selectedRows[targetIndex] = temp;
}
}
})
console.log("After", selectedRows);
注意:您需要確保這兩個變數實際上包含相同的位置
locations并且selectedRows長度相等。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/451562.html
上一篇:創建遍歷資料框列的函式
