Javascript 如何获取具有最近值的数组项的索引?
我有以下json中的对象数组,我想获得数组中对象的索引,该索引通过与给定数值相对的天数属性最接近 在jQuery或JS中是否存在用于此目的的内置函数,或者我如何轻松解决它 谢谢你的帮助Javascript 如何获取具有最近值的数组项的索引?,javascript,jquery,comparison,Javascript,Jquery,Comparison,我有以下json中的对象数组,我想获得数组中对象的索引,该索引通过与给定数值相对的天数属性最接近 在jQuery或JS中是否存在用于此目的的内置函数,或者我如何轻松解决它 谢谢你的帮助 "warningAttributes": [ { "id": "3", "days": 15,
"warningAttributes": [
{
"id": "3",
"days": 15,
"color": "#FFFFFF"
},
{
"id": "4",
"days": 98,
"color": "#343434"
}
]
考虑到您正在尝试查找最接近的ID 你可以这样做。虽然您需要清理代码,但这是可行的
var id=3;
var temp;
var prev;
var x={"warningAttributes": [
{
"id": "3",
"days": 15,
"color": "#FFFFFF"
},
{
"id": "4",
"days": 98,
"color": "#343434"
}
]
}
prev=x.warningAttributes[0].id;
val=prev;
$(x.warningAttributes).each(function(i,ele){
if(id>parseInt(ele.id)){
temp=id-parseInt(ele.id);
}else{
temp=parseInt(ele.id)-id;
}
if(prev>temp)
{prev=temp;
val=ele.id;}
})
console.log(val)
唯一的问题是,如果有一个数字与两个以上的数字具有相同的差异,则返回第一个数字。
例如
3
和4
,如果您找到最接近3.5
,您将得到第一个数字,即3
,,考虑到您正试图找到最接近的ID
你可以这样做。虽然您需要清理代码,但这是可行的
var id=3;
var temp;
var prev;
var x={"warningAttributes": [
{
"id": "3",
"days": 15,
"color": "#FFFFFF"
},
{
"id": "4",
"days": 98,
"color": "#343434"
}
]
}
prev=x.warningAttributes[0].id;
val=prev;
$(x.warningAttributes).each(function(i,ele){
if(id>parseInt(ele.id)){
temp=id-parseInt(ele.id);
}else{
temp=parseInt(ele.id)-id;
}
if(prev>temp)
{prev=temp;
val=ele.id;}
})
console.log(val)
唯一的问题是,如果有一个数字与两个以上的数字具有相同的差异,则返回第一个数字。
例如3
和4
,如果你找到最接近3.5
,你会得到第一个数字,即3
,如果我能很好地理解:
jQuery(document).ready(function () {
var control=40;
var json = {"warningAttributes": [
{
"id": "3",
"days": 15,
"color": "#FFFFFF"
},
{
"id": "4",
"days": 98,
"color": "#343434"
},
{
"id": "5",
"days": 40,
"color": "#343434"
}
]};
$.each(json.warningAttributes, function(k, v) {
var diff;
diff= control - v.days
if(diff==0) alert("this is your element: " + v.id);
})
});
如果我能很好地理解:
jQuery(document).ready(function () {
var control=40;
var json = {"warningAttributes": [
{
"id": "3",
"days": 15,
"color": "#FFFFFF"
},
{
"id": "4",
"days": 98,
"color": "#343434"
},
{
"id": "5",
"days": 40,
"color": "#343434"
}
]};
$.each(json.warningAttributes, function(k, v) {
var diff;
diff= control - v.days
if(diff==0) alert("this is your element: " + v.id);
})
});
如果您不选择[0]
如果您将[0]
简化为以下内容,则这也会为您提供最近的列表:
function getNearest(arr,value)
{
var diff = null;
var index= 0;
$.each(arr,function(i,item) {
var current = Math.abs(item.days - value);
if (diff == null) diff = current;
if (current< diff)
index=i;
});
return index
}
函数getNearest(arr,值)
{
var diff=null;
var指数=0;
$。每个(arr,功能(i,项目){
var current=Math.abs(item.days-值);
如果(diff==null)diff=current;
如果(电流<差值)
指数=i;
});
回报指数
}
就这么简单:
function getNearest(arr,value)
{
var diff = null;
var index= 0;
$.each(arr,function(i,item) {
var current = Math.abs(item.days - value);
if (diff == null) diff = current;
if (current< diff)
index=i;
});
return index
}
函数getNearest(arr,值)
{
var diff=null;
var指数=0;
$。每个(arr,功能(i,项目){
var current=Math.abs(item.days-值);
如果(diff==null)diff=current;
如果(电流<差值)
指数=i;
});
回报指数
}
克里斯几乎偷了这个(+1),但下面是我如何尝试的细目:
function getDays(arr, value) {
// get a list of the differences between the requested value
// and the numbers in the array and make them all positive
var abs = arr.map(function (el) { return Math.abs(el.days - value); });
// find the smallest number
var smallest = Math.min.apply(null, abs);
// find out where that number is in the positive number array
var index = abs.indexOf(smallest);
// return an object that contains the index of the
// object, and the number of days that were closest
return { index: index, value: arr[index].days };
}
var result = getDays(arr, 35); // Object { index: 3, value: 17 }
使用结果获取索引。索引
克里斯几乎偷了这个(+1),但下面是我如何尝试的分类:
function getDays(arr, value) {
// get a list of the differences between the requested value
// and the numbers in the array and make them all positive
var abs = arr.map(function (el) { return Math.abs(el.days - value); });
// find the smallest number
var smallest = Math.min.apply(null, abs);
// find out where that number is in the positive number array
var index = abs.indexOf(smallest);
// return an object that contains the index of the
// object, and the number of days that were closest
return { index: index, value: arr[index].days };
}
var result = getDays(arr, 35); // Object { index: 3, value: 17 }
使用结果获取索引。索引
我开始了,因为写短代码很有趣
var warningAttributes = [{"id": "3", "days": 15, "color": "#FFFFFF"},
{"id": "4", "days": 98, "color": "#343434"}]
var n = 90;
var distances = warningAttributes.map(function(c) {return Math.abs(n - c.days);})
var nearest_idx = distances.indexOf(Math.min.apply(Math, distances));
这是我的目标,因为写短代码很有趣
var warningAttributes = [{"id": "3", "days": 15, "color": "#FFFFFF"},
{"id": "4", "days": 98, "color": "#343434"}]
var n = 90;
var distances = warningAttributes.map(function(c) {return Math.abs(n - c.days);})
var nearest_idx = distances.indexOf(Math.min.apply(Math, distances));
您想获取具有最小天数参数的项目吗?请显示您到目前为止尝试了什么?最接近给定数字的项目。因此,我给出了数字40,最近的是索引为0的对象,有15天。您想获取具有最小天参数的项目吗?请显示您迄今为止尝试的内容?最接近给定数字的项目。所以我给出了数字40,最近的是索引为0的对象,有15天。我刚才回答了,但你的方法更优雅+1:)简洁,但不是索引,它返回值。我刚才回答了,但你的方法更优雅+1:)简洁,但不是索引,它返回值。