根据另一个数组值对对象数组进行排序 Javascript
Sort array of objects depending on another array values Javascript
我有两个数组:
第一个数组:
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
]
]
我需要根据第二个数组 (LOCATIONS)SelectedRows.LATLON 的匹配值对第一个数组 (SelectedRows) 进行排序
我试过:
var Output= LOCATIONS.filter(function(obj) {
return SelectedRows.LATLON.indexOf(obj) == -1;
});
但是没有用。
因此,如果有人可以给我建议,我将不胜感激。
抱歉我的英语不好,为了让我知道我发布了预期的输出:
预期输出:
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"
}]
感谢您的帮助
如果您在 selectedRows
和 locations
中都有 n
个元素,那么在 O(n)
时间内执行此操作的最快方法(至少渐进)如下。
- 遍历正确顺序的位置并将位置值映射到它们(应该)所在的索引。
您不能为此使用数组,因为无法使用
Map
内部使用的 Object.is()
来比较数组。所以你可以在这里使用一个连接的字符串。可能有更好/更清洁的选择,但这很简单并且可以完成工作。这需要 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
并为每个对象确定它的键,这又是连接的位置,并使用该键获取对象在 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);
Note: you would need to make sure that the two variables locations
and selectedRows
actually contain the same locations and are of equal length.
你应该做的是 map LOCATIONS
在地图中 find 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; }
以下代码有点脏,但它有效:
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);
我有两个数组: 第一个数组:
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
]
]
我需要根据第二个数组 (LOCATIONS)SelectedRows.LATLON 的匹配值对第一个数组 (SelectedRows) 进行排序
我试过:
var Output= LOCATIONS.filter(function(obj) {
return SelectedRows.LATLON.indexOf(obj) == -1;
});
但是没有用。 因此,如果有人可以给我建议,我将不胜感激。 抱歉我的英语不好,为了让我知道我发布了预期的输出:
预期输出:
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"
}]
感谢您的帮助
如果您在 selectedRows
和 locations
中都有 n
个元素,那么在 O(n)
时间内执行此操作的最快方法(至少渐进)如下。
- 遍历正确顺序的位置并将位置值映射到它们(应该)所在的索引。
您不能为此使用数组,因为无法使用
Map
内部使用的Object.is()
来比较数组。所以你可以在这里使用一个连接的字符串。可能有更好/更清洁的选择,但这很简单并且可以完成工作。这需要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
并为每个对象确定它的键,这又是连接的位置,并使用该键获取对象在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);
Note: you would need to make sure that the two variables
locations
andselectedRows
actually contain the same locations and are of equal length.
你应该做的是 map LOCATIONS
在地图中 find 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; }
以下代码有点脏,但它有效:
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);