-2

I have a problem in a mock server i am doing. I use npm faker to have some random data, and i go get that data to another js file. Now i want to have a json structure like this:

{
 0: 
  varOne: 'value'
 1:
  varTwo: 'value
}

and at the moment i am getting this json structure:

{
 file1: {
   0: 
     varOne: 'value'
   1:
     varTwo: 'value
   },
file2: {
   0: 
     varOne: 'value'
   1:
     varTwo: 'value
   }
}

Basically i have two js files who i want to merge in one but resulting with no sub-levels in the Json structure. At the moment my code is this:

const file1 = generateFakeObject(nameOfFile1, 4)
const file2 =  generateFakeObject(nameOfFile2, 8)
data.jsonStructure = {file1, file2};

Can anyone help me please???

  • That doesn't look like a valid JSON structure. what is `0: varOne: 'value'`? – Nick Nov 24 '19 at 23:45
  • Does this answer your question? [Merge two json/javascript arrays in to one array](https://stackoverflow.com/questions/10384845/merge-two-json-javascript-arrays-in-to-one-array) – Jay Nov 25 '19 at 06:22

1 Answers1

0

JSON structure you provided in OP is not looks like a valid JSON.

Do you mean ?

{
    "file1": {
        "varOne": "value",
        "varTwo": "value"
    },
    "file2": {
        "varOne": "value",
        "varTwo": "value"
    }
}

If Yes, Working Demo :

var jsonObj = {
 "file1": {
  "varOne": "value",
  "varTwo": "value"
 },
 "file2": {
  "varOne": "value",
  "varTwo": "value"
 }
};

var obj = {};

obj = {...jsonObj.file1, ...jsonObj.file2};

console.log(obj);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123