在 Javascript 中的对象数组中查找最接近数字的索引
Find the index of closest number in array of objects in Javascript
我为此使用 javascript。
我有一个数据集如下:
var filteredValues = [{
type1 : [ ],
type2 : [{w:86,h:108,l:66}, {w:86,h:109,l:66}],
type3 : [{w:82,h:110,l:73}]
}]
我希望能够根据用户提供的 h
值将此数据集过滤到最接近的项目。
示例:如果用户将 105
提供为 h
,则会发生以下情况
type1
将被丢弃,因为此时没有选择任何内容。
105
将匹配 type2
和 type3
的 h
值,选择最接近的那个。在这种情况下,{w:86,h:108,l:66}
from type2
as 105
最接近或等于 109
或 110
(来自 type3
).
- 输出的结果将是类型名称,在这种情况下
type2
如有错误欢迎指正。另外,如果您认为更改数据集可以使它更容易,请告诉我。
以下是一些可能有用的有趣话题
更新:
我尝试了以下方法
function getResult(filteredValues, 105){
filteredValues.reduce((acc, obj) =>
Math.abs(y - obj.s) < Math.abs(y - acc.s) ? obj : acc
);
}
非常感谢您。
此解决方案仅采用给定数据的对象。
有了条目,
- 过滤空数组
- 将数组缩减后的结果映射到目标值上,取绝对delta取最小可能值
最后从条目创建一个新对象。
const
data = { type1: [], type2: [{ w: 86, h: 108, l: 66 }, { w: 86, h: 109, l: 66 }], type3: [{ w: 82, h: 110, l: 73 }] },
height = 105,
result = Object.fromEntries(Object
.entries(data)
.filter(([_, { length }]) => length)
.map(([k, v]) => [k, v.reduce((a, b) => Math.abs(a.h - height) <= Math.abs(b.h - height)
? a
: b
)])
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
我刚刚写了这段代码,它似乎可以工作,但它需要更多的测试(它更大但可能更容易理解):
const filteredValues = [
{
type1: [],
type2: [
{ w: 86, h: 108, l: 66 },
{ w: 86, h: 109, l: 66 },
],
type3: [{ w: 82, h: 106, l: 73 }],
},
];
function filter(h) {
const keys = Object.keys(filteredValues[0]);
const values = Object.values(filteredValues[0]);
const length = keys.length;
let tempType = keys[0];
let tempClosestHValue = values[0][0]?.h ?? Infinity;
for (let i = 0; i < length; i++) {
const type = values[i];
const key = keys[i];
if (type.length > 0) {
let closestHValue = Math.abs(type[0].h - h);
for (let y = 0; y < values[i].length; y++) {
let data = values[i][y];
if (Math.abs(data.h - h) < closestHValue) {
closestHValue = data.h;
}
}
if (closestHValue < tempClosestHValue) {
tempClosestHValue = closestHValue;
tempType = key;
}
}
}
return tempType;
}
console.log(filter(105));
which returns "type3", 但是如果你修改第三个类型的 h
属性 为110,你得到 "type2" 作为输出。
我为此使用 javascript。 我有一个数据集如下:
var filteredValues = [{
type1 : [ ],
type2 : [{w:86,h:108,l:66}, {w:86,h:109,l:66}],
type3 : [{w:82,h:110,l:73}]
}]
我希望能够根据用户提供的 h
值将此数据集过滤到最接近的项目。
示例:如果用户将 105
提供为 h
,则会发生以下情况
type1
将被丢弃,因为此时没有选择任何内容。105
将匹配type2
和type3
的h
值,选择最接近的那个。在这种情况下,{w:86,h:108,l:66}
fromtype2
as105
最接近或等于109
或110
(来自type3
).- 输出的结果将是类型名称,在这种情况下
type2
如有错误欢迎指正。另外,如果您认为更改数据集可以使它更容易,请告诉我。
以下是一些可能有用的有趣话题
更新:
我尝试了以下方法
function getResult(filteredValues, 105){
filteredValues.reduce((acc, obj) =>
Math.abs(y - obj.s) < Math.abs(y - acc.s) ? obj : acc
);
}
非常感谢您。
此解决方案仅采用给定数据的对象。
有了条目,
- 过滤空数组
- 将数组缩减后的结果映射到目标值上,取绝对delta取最小可能值
最后从条目创建一个新对象。
const
data = { type1: [], type2: [{ w: 86, h: 108, l: 66 }, { w: 86, h: 109, l: 66 }], type3: [{ w: 82, h: 110, l: 73 }] },
height = 105,
result = Object.fromEntries(Object
.entries(data)
.filter(([_, { length }]) => length)
.map(([k, v]) => [k, v.reduce((a, b) => Math.abs(a.h - height) <= Math.abs(b.h - height)
? a
: b
)])
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
我刚刚写了这段代码,它似乎可以工作,但它需要更多的测试(它更大但可能更容易理解):
const filteredValues = [
{
type1: [],
type2: [
{ w: 86, h: 108, l: 66 },
{ w: 86, h: 109, l: 66 },
],
type3: [{ w: 82, h: 106, l: 73 }],
},
];
function filter(h) {
const keys = Object.keys(filteredValues[0]);
const values = Object.values(filteredValues[0]);
const length = keys.length;
let tempType = keys[0];
let tempClosestHValue = values[0][0]?.h ?? Infinity;
for (let i = 0; i < length; i++) {
const type = values[i];
const key = keys[i];
if (type.length > 0) {
let closestHValue = Math.abs(type[0].h - h);
for (let y = 0; y < values[i].length; y++) {
let data = values[i][y];
if (Math.abs(data.h - h) < closestHValue) {
closestHValue = data.h;
}
}
if (closestHValue < tempClosestHValue) {
tempClosestHValue = closestHValue;
tempType = key;
}
}
}
return tempType;
}
console.log(filter(105));
which returns "type3", 但是如果你修改第三个类型的 h
属性 为110,你得到 "type2" 作为输出。