Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!