Home > Software design >  How do I merge 2 properties of 1 JSON file if the key name is the same?
How do I merge 2 properties of 1 JSON file if the key name is the same?

Time:02-11

I have the following JSON:

[
    {
        "computers": {
            "available": [
                6,
                2
            ],
            "held": [],
            "upgrades": [],
            "installed": [
                "PC-1"
            ]
        },
        "version": "0.6.55-0ubuntu12~20.04.4",
        "name": "accountsservice",
        "summary": "query and manipulate user account information"
    },
    {
        "computers": {
            "available": [
                209,
                211,
                210
            ],
            "held": [],
            "upgrades": [],
            "installed": [
                "PC-3"
            ]
        },
        "version": "0.6.45-1ubuntu1.3",
        "name": "accountsservice",
        "summary": "query and manipulate user account information"
    }
]

I want to make a check against the data to check to see if the name property has any duplicate values. As such above, there are 2 accountservices but they have different version values.

How would I combine the 2 so that I only have 1 accountservices but 2 version in it using ReactJS? The other data is not relevant so can either be removed or merged also, I am only interested in listing different version for each name.

Expected output:

[
    {
        "version": "0.6.55-0ubuntu12~20.04.4, 0.6.45-1ubuntu1.3",
        "name": "accountsservice",
        "summary": "query and manipulate user account information"
    }
]

I have tried using the following function that I found online but can't get it working:

const sanitize = (source_array) => { 
      let output = [];
  
      source_array.forEach( e => {
        if (output.filter( e1 => e1.name === e.name ).length === 0) {
         let unique_e = source_array.filter(j => j.name === e.name );
         let model = {};
        if (unique_e.length === 1)
          model = e;
        else {
          model.name = e.name;
          model.computers.installed = e.computers.installed;
          model.version = [];
          for ( const el of unique_e ) {
              const { version } = el;
              model.version.push({version});
        }
      }
     output.push(model)
     }
    });
      return output 
    }

CodePudding user response:

This is a perfect use case for the reduce function

enter image description here

CodePudding user response:

This would get the job done

let source_array = [
  {
    computers: {
      available: [6, 2],
      held: [],
      upgrades: [],
      installed: ["PC-1"]
    },
    version: "0.6.55-0ubuntu12~20.04.4",
    name: "accountsservice",
    summary: "query and manipulate user account information"
  },
  {
    computers: {
      available: [209, 211, 210],
      held: [],
      upgrades: [],
      installed: ["PC-3"]
    },
    version: "0.6.45-1ubuntu1.3",
    name: "accountsservice",
    summary: "query and manipulate user account information"
  }
];

let output = [];

source_array.map((item) => {
  let filter = output.filter((i) => i.name === item.name);
  if (filter.length) {
    filter[0].version  = `, ${item.version}`;
  } else {
    output.push({
      version: item.version,
      name: item.name,
      summary: item.summary
    });
  }
});

console.log(output);

CodePudding user response:

This would also work. This solution uses the name for grouping and lists the versions for each name and groups computers under different versions.

const input = [ { computers: { available: [6, 2], held: [], upgrades: [], installed: ["PC-1"], }, version: "0.6.55-0ubuntu12~20.04.4", name: "accountsservice", summary: "query and manipulate user account information", }, { computers: { available: [209, 211, 210], held: [], upgrades: [], installed: ["PC-3"], }, version: "0.6.45-1ubuntu1.3", name: "accountsservice", summary: "query and manipulate user account information", }, ];

const output = Object.entries(
  input.reduce((prev, { name, version, computers }) => {
    // check for the same name
    if (prev[name]) {
      prev[name].version.push(version);
      // check for the same version of computers in under same name
      if (prev[name].computers[version]) {
        prev[name].computers[version].push(computers);
      } else {
        prev[name].computers[version] = [computers];
      }
    } else {
      // name is identified for the first time
      // create a new entry for that
      prev[name] = {
        version: [version],
        computers: { [version]: [computers] },
      };
    }
    return prev;
  }, {})
).map(([key, { version, computers }]) => ({
  name: key,
  versions: version,
  computers,
}));

console.log(output);

  • Related