-1

I have a common field in each JSON object, i.e Field A. And I have Variable fields depending on data from service, i.e,(Variable Field A and Variable Field B).

I have a JSON data in this below format:

[{
    "Field A": "ABC",
    "Variable Field A": "66"
},
{
    "Field A": "DEF",
    "Variable Field A": "70"
},
{
    "Field A": "GHI",
    "Variable Field A": "135"
},
{
    "Field A": "JKL",
    "Variable Field A": "19"
},
{
    "Field A": "ABC",
    "Variable Field B": "-729"
},
{
    "Field A": "GHI",
    "Variable Field B": "962"
},
{
    "Field A": "DEF",
    "Variable Field B": "334"
},
{
    "Field A": "JKL",
    "Variable Field B":"241"
}]

I need to put the variable fields together based on common field (here Field A), so that all variable fields for one common field is available within one object. How can I make my JSON converted to something like this?

[{
    "Field A": "ABC",
    "Variable Field A": "66",
    "Variable Field B": "-729"
},
{
    "Field A": "DEF",
    "Variable Field A": "70",
    "Variable Field B": "334"
},
{
    "Field A": "GHI",
    "Variable Field A": "135",
    "Variable Field B": "962"
},
{
    "Field A": "JKL",
    "Variable Field A": "19",
    "Variable Field B": "241
}]
  • Loop original array and build new array based on your condition – Justinas Jul 12 '22 at 09:18
  • Parse the JSON data, group the array elements by property and serialize the result. – jabaa Jul 12 '22 at 09:24
  • 1
    If you search this website for "group array of objects by property value", you will find plenty of results. – IT goldman Jul 12 '22 at 09:25
  • Does this answer your question? [Group array of objects by string property value in JavaScript?](https://stackoverflow.com/questions/47280159/group-array-of-objects-by-string-property-value-in-javascript) – jabaa Jul 12 '22 at 09:26

4 Answers4

0
let response = array.reduce((storage,objectField)=>{
  let [ [fieldKey, fieldValue],[fieldVariableKey,fieldVariableValue]] = Object.entries(objectField);
  if(!storage[fieldKey+fieldValue] ){
    storage[fieldKey+fieldValue]=objectField
  }else{
    storage[fieldKey+fieldValue][fieldVariableKey]=fieldVariableValue
  }
  return storage
},{})

response = Object.values(response)

console.log(response)

this code takes any possible fields into consideration. the data can decide to have more fields like Field B ,C or D so this code does not only check fot Field A but for all possible fields. i hope this helps

-1

Decode your json in the the proper datastructure and recreate your array of objects using the proper algorithm for the matching.

Something like:

  1. take the first object
  2. save the Field A
  3. iterate other objects and check for the field A
  4. if you find another Field A add the new "Variable Field B" in the new object structure with the found value or the new "Variable Field A" with the found value.
  5. delete the found items in the original array (in order to prevent duplications)
  6. next iteration

If you're using javascript for that check .hasOwnProperty("propertyName") method that check if a field exists in the object. It should be usefull for checking if you have to get "Variable Field A" or "Variable Field B" from the object.

Gicu Aftene
  • 502
  • 2
  • 9
  • I did the same thing like you suggested, it is working, but does it affect performance if I put two nested loops on huge dataset? – Tarun Sai Jul 13 '22 at 10:18
  • @TarunSai Yes it has ... that it's not an optimal algoritm to do that... the best solution would be to scan all elements and insert every property in a new object where you associate the key to an another object containing the merged proprieties of the exisisting key - object association. after that iterate the resulted object and copy the elements into an array dropping the key – Gicu Aftene Jul 13 '22 at 10:55
  • @TarunSai in that way you will have the initial array -> the mapped grouped object -> then the result array in the form you asked – Gicu Aftene Jul 13 '22 at 10:56
-1

This is a great application for the Array reducer function. Try this:

const data = [] // your JSON data
data.reduce((acc,item)=>({
  ...acc,
  [item['Field A']]: {...acc[item['Field A']],...item}
}),{})

The result:

{
  ABC: {
    'Field A': 'ABC',
    'Variable Field A': '66',
    'Variable Field B': '-729'
  },
  DEF: {
    'Field A': 'DEF',
    'Variable Field A': '70',
    'Variable Field B': '334'
  },
  GHI: {
    'Field A': 'GHI',
    'Variable Field A': '135',
    'Variable Field B': '962'
  },
  JKL: {
    'Field A': 'JKL',
    'Variable Field A': '19',
    'Variable Field B': '241'
  }
}
SteveGreenley
  • 630
  • 1
  • 5
  • 7
-1

you can do something like this

const group = (data, field) => Object.values(data.reduce((res, item) => {
  const existing = res[item[field]] || {}
  
  return {
    ...res,
    [item[field]]: {...existing, ...item}
  }

}, {}))


const data = [{
    "Field A": "ABC",
    "Variable Field A": "66"
},
{
    "Field A": "DEF",
    "Variable Field A": "70"
},
{
    "Field A": "GHI",
    "Variable Field A": "135"
},
{
    "Field A": "JKL",
    "Variable Field A": "19"
},
{
    "Field A": "ABC",
    "Variable Field B": "-729"
},
{
    "Field A": "GHI",
    "Variable Field B": "962"
},
{
    "Field A": "DEF",
    "Variable Field B": "334"
},
{
    "Field A": "JKL",
    "Variable Field B":"241"
}]

const result = group(data, 'Field A')

console.log(result)
R4ncid
  • 6,944
  • 1
  • 4
  • 18