0

I have a json object response from serve as like below structure, I have to parse the data from this result.

[CGOLD-1: Array(6), CGOLD-2: Array(6), MGOLD-1: Array(6), MGOLD-2: Array(6), MGOLD-3: Array(6), …]

enter image description here

enter image description here

I have tried as like this to check, My type script code,

baserates: Object = {};
oldbaserates: Object = {};
this.liverateservice.setcallback((data)=>{
this.baserates = data;
this.baserates.forEach((item) => {
        console.log(item);
        //newArray.push(Object.assign({}, item));
    });
});

but my console not working this, I have to do clone object as like this

deepClone(oldArray: Object[]) {
    let newArray: any = [];
    oldArray.forEach((item) => {
      newArray.push(Object.assign({}, item));
    });
    return newArray;
  }

Could you please help anyone to process this type of data structure in angular 2.

Vinoth Kumar
  • 489
  • 3
  • 17
  • 45
  • what is the variable that you are using to store the value of the json response? – Anuradha Gunasekara May 16 '18 at 07:43
  • Please show us what the JSON looks like. I have no idea what that snippet at the beginning of your question is, but it sure doesn't look like an array. – JLRishe May 16 '18 at 07:49
  • 1
    It also doesn't look like you're using the `data` variable anywhere. Please show us a code example that clearly illustrates what you are trying to do. You've failed to explain what any of these variables are or what you're trying to do. – JLRishe May 16 '18 at 07:53
  • I have updated data structure again, Could you please check it again. – Vinoth Kumar May 16 '18 at 07:57
  • 1
    What is your expected output? – BlackBeard May 16 '18 at 08:05
  • It looks like you have an array with property names. Is that what's being sent from the API you're calling? It's not even possible to represent that in JSON and it's not normal to have such a thing in JavaScript. – JLRishe May 16 '18 at 08:13
  • How do you even get that key-value pair array...? – Carsten May 16 '18 at 08:13
  • Take a look at [this Answer](https://stackoverflow.com/questions/15877362/declare-and-initialize-a-dictionary-in-typescript/15884066#15884066). You are trying to create a Dictionary – P. Moloney May 16 '18 at 08:19
  • I need to deep clone my object to compare last and current values of the bid and ask rates to update color code. Can any one help how to do in this data structure. – Vinoth Kumar May 16 '18 at 13:26

1 Answers1

0

You can flatten an array of arrays with

const flatten = (array: any[][]) => (array ? array.reduce((output, array) => [...output, ...array], []) : array);

const flatArray = flatten(data);

Now the flatArray is just a single array.

You can see a demo of it here

https://stackblitz.com/edit/typescript-wdtvs9

Adrian Brand
  • 20,384
  • 4
  • 39
  • 60