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
CodePudding user response:
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"))
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
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; }
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>