var data= [
{"area":"1","location":"01051A","aisle":"1","sequence":12354,"x":13,"y":1338},
{"area":"1","location":"01071A","aisle":"1","sequence":12355,"x":13,"y":1437},
{"area":"1","location":"01081A","aisle":"1","sequence":12356,"x":173,"y":1437},
{"area":"1","location":"02021A","aisle":"2","sequence":12473,"x":401,"y":1092},
{"area":"1","location":"02022A","aisle":"2","sequence":12472,"x":401,"y":1140},
{"area":"1","location":"02031A","aisle":"2","sequence":12471,"x":241,"y":1191},
{"area":"1","location":"02032A","aisle":"2","sequence":12470,"x":241,"y":1239},
{"area":"1","location":"02041A","aisle":"2","sequence":12469,"x":401,"y":1191},
{"area":"1","location":"02042A","aisle":"2","sequence":12468,"x":401,"y":1239},
{"area":"1","location":"03101A","aisle":"3","sequence":12476,"x":629,"y":1437},
{"area":"1","location":"03102A","aisle":"3","sequence":12477,"x":629,"y":1485},
{"area":"1","location":"03111A","aisle":"3","sequence":12478,"x":469,"y":1536},
{"area":"1","location":"03112A","aisle":"3","sequence":12479,"x":469,"y":1584}
]

Tengo este conjunto de datos. ¿Cuál es la mejor manera de filtrar por cada 'pasillo' y trabajar con ese conjunto? por ejemplo, quiero averiguar la secuencia mínima y máxima para el pasillo 1, hacer algo y luego el pasillo 2 ... hacer algo y así sucesivamente ...

Actualmente obtengo las coordenadas mínimas y máximas para todos los datos usando algo como esto.

 var minX = Math.min.apply(null, data.map(function(a){return a.x;})),
 maxX = Math.max.apply(null, data.map(function(a){return a.x;})),
   minY = Math.min.apply(null, data.map(function(a){return a.y;})),
   maxY  = Math.max.apply(null, data.map(function(a){return a.y;}));

Pero también necesito encontrar el mínimo, máximo y para cada conjunto para poder obtener coordenadas y dibujar etiquetas en la pantalla.

Estaba pensando en hacer un bucle, luego crear una serie de pasillos únicos y luego repetir los datos nuevamente, pero creo que habrá una mejor manera.

Aquí está la JsFiddle en la que estoy trabajando.

2
causita 29 oct. 2017 a las 23:04

4 respuestas

La mejor respuesta

Puede tomar un objeto para recopilar aisle, x, y y sus valores min y max.

var data = [{ area: "1", location: "01051A", aisle: "1", sequence: 12354, x: 13, y: 1338 }, { area: "1", location: "01071A", aisle: "1", sequence: 12355, x: 13, y: 1437 }, { area: "1", location: "01081A", aisle: "1", sequence: 12356, x: 173, y: 1437 }, { area: "1", location: "02021A", aisle: "2", sequence: 12473, x: 401, y: 1092 }, { area: "1", location: "02022A", aisle: "2", sequence: 12472, x: 401, y: 1140 }, { area: "1", location: "02031A", aisle: "2", sequence: 12471, x: 241, y: 1191 }, { area: "1", location: "02032A", aisle: "2", sequence: 12470, x: 241, y: 1239 }, { area: "1", location: "02041A", aisle: "2", sequence: 12469, x: 401, y: 1191 }, { area: "1", location: "02042A", aisle: "2", sequence: 12468, x: 401, y: 1239 }, { area: "1", location: "03101A", aisle: "3", sequence: 12476, x: 629, y: 1437 }, { area: "1", location: "03102A", aisle: "3", sequence: 12477, x: 629, y: 1485 }, { area: "1", location: "03111A", aisle: "3", sequence: 12478, x: 469, y: 1536 }, { area: "1", location: "03112A", aisle: "3", sequence: 12479, x: 469, y: 1584 }],
    values = Object.create(null);

data.forEach(function (o) {
    if (!values[o.aisle]) {
        values[o.aisle] = { x: { min: o.x, max: o.x }, y: { min: o.y, max: o.y } };
        return;
    }
    ['x', 'y'].forEach(function (k) {
        ['min', 'max'].forEach(function (m) {
            values[o.aisle][k][m] = Math[m](values[o.aisle][k][m], o[k]);
        });
    });
});
        
console.log(values);
.as-console-wrapper { max-height: 100% !important; top: 0; }
2
Nina Scholz 29 oct. 2017 a las 20:19

Este es otro sabor usando Array.reduce

var data = [{ area: "1", location: "01051A", aisle: "1", sequence: 12354, x: 13, y: 1338 }, { area: "1", location: "01071A", aisle: "1", sequence: 12355, x: 13, y: 1437 }, { area: "1", location: "01081A", aisle: "1", sequence: 12356, x: 173, y: 1437 }, { area: "1", location: "02021A", aisle: "2", sequence: 12473, x: 401, y: 1092 }, { area: "1", location: "02022A", aisle: "2", sequence: 12472, x: 401, y: 1140 }, { area: "1", location: "02031A", aisle: "2", sequence: 12471, x: 241, y: 1191 }, { area: "1", location: "02032A", aisle: "2", sequence: 12470, x: 241, y: 1239 }, { area: "1", location: "02041A", aisle: "2", sequence: 12469, x: 401, y: 1191 }, { area: "1", location: "02042A", aisle: "2", sequence: 12468, x: 401, y: 1239 }, { area: "1", location: "03101A", aisle: "3", sequence: 12476, x: 629, y: 1437 }, { area: "1", location: "03102A", aisle: "3", sequence: 12477, x: 629, y: 1485 }, { area: "1", location: "03111A", aisle: "3", sequence: 12478, x: 469, y: 1536 }, { area: "1", location: "03112A", aisle: "3", sequence: 12479, x: 469, y: 1584 }];

var retData = data.reduce(function(ret, current) {
    ret[current.aisle] = ret[current.aisle] || {};
    ret[current.aisle]['maxX'] = Math.max(ret[current.aisle]['maxX'], current.x) || current.x;
    ret[current.aisle]['maxY'] = Math.max(ret[current.aisle]['maxY'], current.y) || current.y;
    ret[current.aisle]['minX'] = Math.min(ret[current.aisle]['minX'], current.x) || current.x;
    ret[current.aisle]['minY'] = Math.min(ret[current.aisle]['minY'], current.y) || current.y;
    return ret;
}, {});

console.log(retData);
0
jamesjaya 29 oct. 2017 a las 20:37
var aisles = {};

data.forEach(d => {
  if (!aisles[d.aisle]) {
    aisles[d.aisle] = {
      maxX: d.x,
      minX: d.x,
      maxY: d.y,
      minY: d.y
    };
  } else {  
    aisles[d.aisle].maxX = Math.max(aisles[d.aisle].maxX, d.x);
    aisles[d.aisle].minX = Math.min(aisles[d.aisle].minX, d.x);
    aisles[d.aisle].maxY = Math.max(aisles[d.aisle].maxY, d.y);
    aisles[d.aisle].minY = Math.min(aisles[d.aisle].minY, d.y);
  }
})
0
SimonW 29 oct. 2017 a las 20:29

También sugeriría agrupar cualquier conjunto de 'pasillo' como clave de objeto con esa clave uniq de pasillo, donde valus contendrá una matriz de objetos (ese conjunto), que se ordenarán / ordenarán según su demanda, como la suma de x + y así en.

-2
Yossi 29 oct. 2017 a las 20:29