Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda