Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
Splitting out array into multiple arrays based on id

I have the following JSON sample:

[
    {
        "id": "116621",
        "field": "list18",
        "line": 0,
        "value": "6",
        "changeOrder": 5
    },
    {
        "id": "116621",
        "field": "list16",
        "line": 0,
        "value": "47",
        "changeOrder": 4
    },
    {
        "id": "116621",
        "field": "list17",
        "line": 0,
        "value": "3",
        "changeOrder": 2
    },
    {
        "id": "116622",
        "field": "list16",
        "line": 1,
        "value": "6",
        "changeOrder": 3
    },
    {
        "id": "116623",
        "field": "list18",
        "line": 2,
        "value": "11",
        "changeOrder": 6
    },
    {
        "id": "116623",
        "field": "list16",
        "line": 2,
        "value": "5",
        "changeOrder": 1
    }
]

I am trying to run a process that opens a record (based off the id), update values and then saves it.

However instead of running through each item and duplicating open the same record, it would save time by opening the same id record once, running through each item, and then saving it.

So what i need to do is to return the data like this so that it only opens the record once to update. For instance if it is id "116621" then it would group all the id "116621" so i can update each value that is returned - like this:

[
    {
        "id": "116621",
        "field": "list18",
        "line": 0,
        "value": "6",
        "changeOrder": 5
    },
    {
        "id": "116621",
        "field": "list16",
        "line": 0,
        "value": "47",
        "changeOrder": 4
    },
    {
        "id": "116621",
        "field": "list17",
        "line": 0,
        "value": "3",
        "changeOrder": 2
    }
]

I wont know how many are in the array, nor what ID's they will be. All i know is that it will be a sorted array as I have already coded that bit in.

Any help to get a function to return the single array would really be helpful.

Thank you

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Does this help? You can write a function to filter your data by an id(you can make it dynamic since you don't know what it will be) and do the changes only on the filtered array. You can then return either the entire data or the filtered array.

let data = [
    {
        "id": "116621",
        "field": "list18",
        "line": 0,
        "value": "6",
        "changeOrder": 5
    },
    {
        "id": "116621",
        "field": "list16",
        "line": 0,
        "value": "47",
        "changeOrder": 4
    },
    {
        "id": "116621",
        "field": "list17",
        "line": 0,
        "value": "3",
        "changeOrder": 2
    },
    {
        "id": "116622",
        "field": "list16",
        "line": 1,
        "value": "6",
        "changeOrder": 3
    },
    {
        "id": "116623",
        "field": "list18",
        "line": 2,
        "value": "11",
        "changeOrder": 6
    },
    {
        "id": "116623",
        "field": "list16",
        "line": 2,
        "value": "5",
        "changeOrder": 1
    }
]
function filteredData(data, value){
  let filtered =  data.filter(obj=> obj.id === value)
  //do what you want with filtered data
  filtered.forEach(obj=> obj.value = 0)
  //return data
   return filtered
}
console.log(filteredData(data, "116621"))

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could adopt a generic approach, specifying filter() and update() functions.

The updater function would then be applied to any matching elements, in this example we'll increment the changeOrder of any matching elements.

const input = [ { "id": "116621", "field": "list18", "line": 0, "value": "6", "changeOrder": 5 }, { "id": "116621", "field": "list16", "line": 0, "value": "47", "changeOrder": 4 }, { "id": "116621", "field": "list17", "line": 0, "value": "3", "changeOrder": 2 }, { "id": "116622", "field": "list16", "line": 1, "value": "6", "changeOrder": 3 }, { "id": "116623", "field": "list18", "line": 2, "value": "11", "changeOrder": 6 }, { "id": "116623", "field": "list16", "line": 2, "value": "5", "changeOrder": 1 } ];
 
function updateArray(arr, filter, updater) {
    arr.filter(filter).forEach(updater);
    return arr;
}

const targetId = '116621';
const filter = el => el.id === targetId;
const updater = el => el.changeOrder++;

console.log('Updated:', updateArray(input, filter, updater));
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda