0

I'm trying to create an JSON from another JSON using Javascript.

Here is my code.

var data = [{
    "Id": "123",
    "Name": "Abc",
    "Amount": 110000
  },
  {
    "Id": "567",
    "Name": "DEF",
    "Amount": 98000
  },
  {
    "Id": "345",
    "Name": "XYZ",
    "Amount": 145000
  }
];

finalArray(data);

function finalArray(data) {
  var nArray = [];
  var subArray = new Object();
  subArray.total = 0;
  subArray.records = {};
  subArray.size = 0;
  data.forEach(item => {

    subArray.total += item.Amount;
    subArray.records += item;
    subArray.size += 1;
  });
  nArray.push(subArray);
  console.log(nArray);
};

Here In the final object, In records I'm getting the below output.

records: "[object Object][object Object][object Object][object Object]"

expected output is

records: "[object Object][object Object][object Object]"

where as in my actual data, I've 3 records in the input.

Please let me know where I'm going wrong.

Thanks

4
  • What is the shape of the result supposed to be? Commented Oct 7, 2020 at 11:54
  • In my input I've 3 records, where as In my output, it is showing 4 records. @0stone0 Commented Oct 7, 2020 at 11:54
  • 2
    One of the [object Object]s is because you initialize records with subArray.records = {}; , i.e. an object. Then you are trying to "add" another object with subArray.records += item;. This will result in [object Object][object Object] since you are "concatenating" two objects. It goes without saying that concatenating objects as string is wrong. Commented Oct 7, 2020 at 11:56
  • 2
    There isn't any trace of JSON in the code you posted. JSON is a text representation of some data structure. Your code works with objects and never attempts to produce an external representation of the data they contain. Commented Oct 7, 2020 at 11:56

4 Answers 4

1

This should work.

function finalArray(data) {
  var nArray = [];
  var subArray = new Object();
  subArray.total = 0;
  subArray.records = []; // records must be array
  subArray.size = 0;
  data.forEach(item => {

    subArray.total += item.Amount;
    subArray.records.push(item); // need to push each item to records array
    subArray.size += 1;
  });
  nArray.push(subArray);
  console.log(nArray);
};
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks man. This worked great. Can you please let me know where I went wrong?
Ah #@firatozcevahir... Gotcha. Thanks a ton mate...
1

Code has 2 mistakes.

  1. record should be an array not an object
  2. you need use push method to add data to record. Not addition assignment (+=)

var data = [{
    "Id": "123",
    "Name": "Abc",
    "Amount": 110000
  },
  {
    "Id": "567",
    "Name": "DEF",
    "Amount": 98000
  },
  {
    "Id": "345",
    "Name": "XYZ",
    "Amount": 145000
  }
];

finalArray(data);

function finalArray(data) {
  var nArray = [];
  var subArray = new Object();
  subArray.total = 0;
  subArray.records = [];
  subArray.size = 0;
  data.forEach(item => {

    subArray.total += item.Amount;
    subArray.records.push(item);
    subArray.size += 1;
  });
  nArray.push(subArray);
  console.log(nArray);
};

Comments

1

Change your function to this-

function finalArray(data) {
  var nArray = [];
  var subArray = new Object();
  subArray.total = 0;
  subArray.records=[];
  subArray.size = 0;
  data.forEach(item => {
    
    subArray.total += item.Amount;
    subArray.records += item;    
    subArray.size += 1;
  });
  nArray.push(subArray);
  console.log(nArray);
};

Comments

1

Use an array to hold the original items. Also, you'll need push to add an Object to an Array;

var data = [{
    "Id": "123",
    "Name": "Abc",
    "Amount": 110000
  },
  {
    "Id": "567",
    "Name": "DEF",
    "Amount": 98000
  },
  {
    "Id": "345",
    "Name": "XYZ",
    "Amount": 145000
  }
];

finalArray(data);

function finalArray(data) {

  // New data
  let nArray = {};
  nArray.total = 0;
  nArray.size = 0;
  
  // Create an array inside nArray to hold original items
  nArray.records = [];
  
  data.forEach((item) => {
    nArray.total += item.Amount;
    nArray.size += 1;
    nArray.records.push(item);
  });
 
  console.log(nArray);
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.