Discriminate data according to two attributes

2

I have the following arrangement:

let arreglo=[{'class':1,'var':1, 'valor':50},
            {'class':3,'var':2, 'valor':150},
            {'class':1,'var':2, 'valor':200},
            {'class':1,'var':1, 'valor':60},
            {'class':3,'var':2, 'valor':520},
            {'class':1,'var':2, 'valor':250}];

I want to discriminate by sorting according to the attribute 'class'

 [{'class':1,'var':1, 'valor':50},
        {'class':1,'var':2, 'valor':200},
        {'class':1,'var':1, 'valor':50},
        {'class':1,'var':2, 'valor':250}]

        [{'class':3,'var':2, 'valor':150},
        {'class':3,'var':2, 'valor':520}];

then discriminate according to the attribute 'var' to obtain

[{'class':1,'var':1, 'valor':50},
         {'class':1,'var':1, 'valor':50},
         {'class':1,'var':2, 'valor':200},
        {'class':1,'var':2, 'valor':250}]
        [{'class':3,'var':2, 'valor':150},
        {'class':3,'var':2, 'valor':520}];

my code is the following but what I do is discriminate and then I would have to join the arrangements, I want a more efficient way.

let arreglo = [{'class': 1,'var': 1,'valor': 50},
  {'class': 3, 'var': 2,'valor': 150  },
  {'class': 1, 'var': 2,'valor': 200  },
  {'class': 1, 'var': 1,'valor': 60   },
  {'class': 3, 'var': 2,'valor': 520 },
  {'class': 1, 'var': 2,'valor': 250 }
];

function filtrarSegunVariable(arreglo, variable, condicion) {
  return arreglo.filter(function(x) {
    if (x[variable] === condicion) {
      return x;
    }
  })
}
let class1 = filtrarSegunVariable(arreglo, 'class', 1);
let class3 = filtrarSegunVariable(arreglo, 'class', 3);
console.log(filtrarSegunVariable(class1, 'var', 1));
console.log(filtrarSegunVariable(class1, 'var', 2));
console.log(filtrarSegunVariable(class3, 'var', 1));
console.log(filtrarSegunVariable(class3, 'var', 2));
  

note: values 1, 2, 3 must be strings

    
asked by x-rw 22.07.2018 в 08:34
source

2 answers

1

You can pass a comparison function to the sort method. So what you could do is pass a function that first orders comparing a value (class) and, if they are equal, order comparing for a second value (var) and, if they are equal, order comparing for a third value (value).

Something like this:

function(a,b) {
  // primer nivel: usando class
  if (a.class < b.class) {
    return -1;
  } else if (a.class == b.class) {
    // si son iguales = segundo nivel: usando var
    if (a.var < b.var) {
      return -1;
    } else if (a.var == b.var) {
      // si son iguales = tercer nivel: usando valor
      return a.valor >= b.valor;
    } else {
      return 1;
    }
  } else {
    return 1;
  }
}

Here you can see it working:

let arreglo = [
  {'class': 1, 'var': 1, 'valor': 50  },
  {'class': 3, 'var': 2, 'valor': 150 },
  {'class': 1, 'var': 2, 'valor': 200 },
  {'class': 1, 'var': 1, 'valor': 60  },
  {'class': 3, 'var': 2, 'valor': 520 },
  {'class': 1, 'var': 2, 'valor': 250 }
];

var arreglo2 = arreglo.sort(function(a,b) {
  if (a.class < b.class) {
    return -1;
  } else if (a.class == b.class) {
    if (a.var < b.var) {
      return -1;
    } else if (a.var == b.var) {
      return a.valor >= b.valor;
    } else {
      return 1;
    }
  } else {
    return 1;
  }
});

console.log(arreglo2);

And here you can see how it also works if the values were text strings:

let arreglo = [
  {'class': '1', 'var': 'A', 'valor': 50  },
  {'class': '3', 'var': 'B', 'valor': 150 },
  {'class': '1', 'var': 'B', 'valor': 200 },
  {'class': '1', 'var': 'A', 'valor': 60  },
  {'class': '3', 'var': 'B', 'valor': 520 },
  {'class': '1', 'var': 'B', 'valor': 250 }
];

var arreglo2 = arreglo.sort(function(a,b) {
  if (a.class < b.class) {
    return -1;
  } else if (a.class == b.class) {
    if (a.var < b.var) {
      return -1;
    } else if (a.var == b.var) {
      return a.valor >= b.valor;
    } else {
      return 1;
    }
  } else {
    return 1;
  }
});

console.log(arreglo2);
    
answered by 22.07.2018 / 17:35
source
2

To manage arrays there are many functions that can be used without having to resort to the for or the while in this case you can use the Array.prototype.sort method.

The way this function is used is as follows:

1) I declare my array:

 var myArray=[1,4,2,5,7,3];

2) I order it:      myArray.sort ((a, b) = > {           return a-b;      });

the result would be:      [1,2,3,4,5,7] What goes inside the sort is a callback that indicates how the arrangement should be ordered, for more information see this link

Then to order your array you only have to adapt the callback to your needs:

 let arreglo=[{'class':1,'var':1, 'valor':50},
              {'class':3,'var':2, 'valor':150},
              {'class':1,'var':2, 'valor':200},
              {'class':1,'var':1, 'valor':60},
              {'class':3,'var':2, 'valor':520},
              {'class':1,'var':2, 'valor':250}];

 //primero ordenas por el parametro var, porque si ordenas por class primero
 //puede que al ordenar por var se desorganicen las class

 arreglo.sort((a,b)=>{return (a.var-b.var)});
 arreglo.sort((a,b)=>{return a.class-b.class});

 console.log(arreglo);

and your arrangement is ordered.

    
answered by 22.07.2018 в 17:22