0

I pretty new to angular and I have a question. I need 2 arrays for ng2-charts, one with labels and one with data.

I make http request to AWS and I got this json

 {
    "System1": [
        {
            "name": "MF3",
            "descr": "Multifilo MF3",
            "speed": [1,2,3,4],
            "time": ["10.01", "10.02", "10.03", "10.04"]
        }
    ]
}

I assing all the result to result: Array<SystemModel>;

For use speed and time on ng2-charts I have to copy the two array speed and time on new array: public lineChartSpeed: Array<number> = []; and public lineChartTime: Array<any> = [];

How can I copy this 2 array on my new array? I know how to access to data only on html template, but not on typscript file...

My component is:

public lineChartSpeed: Array<number> = [];
lineChartTime: Array<any> = [];

result: Array<ImpiantoModel>;

  getdata() {
    this.http.get<SystemModel[]>(this.myUrl)
      .subscribe(
        data => { this.result =  data;

           // perform the copy of speed and time on lineChartTime and lineChartSpeed
          });

  }

How can I copy the array? If you need more details, please ask in the comments! thank you !

3 Answers 3

2
var system1 = {
    "System1": [
        {
            "name": "MF3",
            "descr": "Multifilo MF3",
            "speed": [1,2,3,4],
            "time": ["10.01", "10.02", "10.03", "10.04"]
        }
    ]
}
var speed = system1.System1[0].speed
var time = system1.System1[0].time
console.log('Array of Speed', speed)
console.log('Array of Time', time)

//Merge or concatenate two Arrays
var newArray = [...speed, ...time]
console.log('Merged or concatenated Arrays', newArray)
Sign up to request clarification or add additional context in comments.

6 Comments

It works!! Thanks, it Works! now I can manage my data!! I really appreciate your help! Can I ask you one more question? on your code there is: System1, How can I copy the array time and speed if I change this value? maybe for other system it can be System2... thank again
Am so sorry, i don't get your question. Do you mean if the object like System1 is changed?
no problem. I have 5 systems and when I make http get I have "System1": [....], , "System2": [....], , "System3": [....], , "System4": [....], how can I tell to mycomponent.ts it is different? On my component I have public name = 'System1' or public name = 'System2' according to the system i'm requesting. thanks
Valid Question. You can get all the keys from object in Array with Object.keys(system1) Object.keys(system1) will return array like ['System1', 'System2', 'System3'], then you can use loop to get all the items. Did i make it clear?
very clear! but I have an error. I wrote: const tmp = Object.keys(data); console.log(tmp); and it print on console log correctly System1 but if I put on var speed = system1.tmp[0].speed . On compiler I have this error: TS2339: Property 'tmp' does not exist on type 'SystemModel'. ps: I gave you best answer, I really appreciate!
|
1

Use slice operator to create a new copy of the array

     this.result =  data.slice();

3 Comments

but I need a copy of speed and time not the entire array! and when I get a copy of all array, how can I continue for have single array?
then you have to slice the time and speed sperately an assign to a variable like
var result ={time:system1.System1[0].time.slice(),speed:system1.System1[0].speed.slice()}
0
this.lineChartSpeed = [].concat(this.result[0].speed);
this.lineChartTime = [].concat(this.result[0].time);

3 Comments

With your code I have and error: error TS2339: Property 'System1' does no t exist on type 'SystemModel[]'.... I alreadytried this solution!
answer edited @NicolòScapin If that does not work, could you please console the data param?
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!

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.