Answer the question
In order to leave comments, you need to log in
How to properly process array data? .filter() method?
Hello, please tell me:
Task:
1. There is an associative array with such objects: (we are talking about hundreds of such objects inside)
arr: [ { name: ' SB ', id: 'O5', value: 10,}, { name: ' SB ', id 'O5', value: 11,} ]
options: [{
value: 'O1',
label: 'Первый'
},{
value: 'O7',
label: 'Второй'
}]
RSUM: function (){
var response = this.arr;
var va = this.value;
var defaul = this.defaultPrograms;
var rates = response.filter(function(elem) {
return elem.id == va;
});
if (rates.length == 0) {
rates = defaul;
console.log(rates);
};
console.log(rates.length);
var minEl = rates[0].value
var br = rates.filter(function(elem){
return elem.value <= minEl;
});
console.log(br);
this.ValueMin = br[0].value;
console.log(this.ValueMin);
}
Answer the question
In order to leave comments, you need to log in
why did you decide that the first element will be minimal?
here we need a sort method rather than a filter
var br = rates.filter(function(elem){
return elem.value <= minEl;
});
var rates = response.reduce(function (acc, elem) {
if (elem.id === va) {
if (!acc) {
return elem.value;
}
return elem.value < acc ? elem.value : acc;
}
return acc;
}, 0);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question