[英]Get data from multidimensional array in JavaScript
I have the following array: 我有以下数组:
var data = [{
length: 900,
fields: 3
},{
length: 1150,
fields: 4
},{
length: 1700,
fields: 5
}];
Now I would like to have a function that returns the fields depending on the given length like: 现在,我想拥有一个根据给定长度返回字段的函数,例如:
function getFields(length) {
// return "3" if length <= 900
// return "4" if length <= 1150
// return "5" if length <= 1700
}
How could I achieve this? 我怎样才能做到这一点?
As long as data is properly sorted, it is a simple for loop 只要数据正确排序,它就很简单
var data = [{ length: 900, fields: 3 },{ length: 1150, fields: 4 },{ length: 1700, fields: 5 }]; function getFields (value) { var i; for (i=0; i<data.length; i++) { if (value <= data[i].length) return data[i].fields; // exit since we found first match } return 0; // what ever the default is for no match } console.log(800, getFields(800)); console.log(900, getFields(900)); console.log(1000, getFields(1000)); console.log(1500, getFields(1500)); console.log(2000, getFields(2000));
or with modern array methods you can use find() which is like a for loop code above under the hood: 或使用现代数组方法,您可以使用find(),类似于上面的for循环代码:
var data = [{ length: 900, fields: 3 },{ length: 1150, fields: 4 },{ length: 1700, fields: 5 }]; function getFields (value) { var i; var match = data.find(function(item) { return value <= item.length }) return match ? match.fields : 0; } console.log(800, getFields(800)); console.log(900, getFields(900)); console.log(1000, getFields(1000)); console.log(1500, getFields(1500)); console.log(2000, getFields(2000));
Now if the data array is out of order, than it should be sorted. 现在,如果数据数组出现故障,则应该对其进行排序。
you can iterate the data
and match the conditon 您可以迭代
data
并匹配条件
var data = [{ length: 900, fields: 3 },{ length: 1150, fields: 4 },{ length: 1700, fields: 5 }]; function getFields(len) { var fields = ''; $.each(data, function(key,value) { if(value.length<=len) fields = value.fields; }); return fields; } // call function alert(getFields(1700));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
You don't need jQuery
for this, it can be done with eg a standard .find()
call. 您不需要
jQuery
,可以通过标准的.find()
调用来完成。 Note that this assumes that the data is sorted by .length
as in your example. 请注意,这假设您的示例中的数据按
.length
排序。
var data = [{ length: 900, fields: 3 }, { length: 1150, fields: 4 }, { length: 1700, fields: 5 }]; var value = 950; var matching = data.find(x => value <= x.length); var fields = matching ? matching.fields : 0; console.log(fields);
I'd define it like so: 我会这样定义它:
function getFields(length) {
var d = data
.filter(d => d.length <= length) // get the list of matching objects
.sort((a, b) => b.length - a.length) // sort descending so largest value is at the front of the array
.shift(); // get the first element from the array
return (d !== undefined) ? d.fields : undefined;// if that element exists, return .fields, otherwise undefined
}
In action: 实际上:
var data = [{ length: 900, fields: 3 },{ length: 1150, fields: 4 },{ length: 1700, fields: 5 }]; function getFields(length) { var d = data .filter(d => d.length <= length) // get the list of matching objects .sort((a, b) => b.length - a.length) // sort descending so largest value is at the front of the array .shift(); // get the first element from the array return (d !== undefined) ? d.fields : undefined;// if that element exists, return .fields, otherwise undefined } var tests = [1700, 1150, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700]; console.log(tests.map(getFields));
While I don't know if this is performant enough for your current use case, but it's relatively readable and easy-to-follow (although this could be made more efficient if the data were always ordered by length
, for instance). 虽然我不知道这对于您当前的用例而言是否足够有效,但是它相对易读且易于遵循(例如,如果始终按
length
对数据进行排序,则可以提高效率)。 If you need something more performant, you could do something like this instead: 如果您需要性能更高的产品,可以执行以下操作:
function getFields(length) {
let d;
let i = data.length - 1;
while (i > -1 && d === undefined) {
if (data[i].length <= length) {
d = data[i].fields;
}
i -= 1;
}
return d;
}
In action: 实际上:
var data = [{ length: 900, fields: 3 },{ length: 1150, fields: 4 },{ length: 1700, fields: 5 }]; function getFields(length) { let d; let i = data.length - 1; while (i > -1 && d === undefined) { if (data[i].length <= length) { d = data[i].fields; } i -= 1; } return d; } var tests = [1700, 1150, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700]; console.log(tests.map(getFields));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.