0

I use angular cli 6.

How can i transform this first array to the second array ?

[Array(4)]
 0:(2) ["a", "b"]


 (2) ["a", "b"]
    0:"a"
    1:"b"

I try this :

   var newArr = [...this.ppssToDisplay.effetsind[0]];
 console.log(this.ppssToDisplay.effetsind) ;
 console.log(newArr) ;
 this.ppsForm.controls['requirements'].setValue(newArr);

I obtain :

console.log(this.ppssToDisplay.effetsind) ;

[Array(1)]
0:Array(1)
0:(2) ["Troubles de la vessie", "Troubles de l'érection"]
length:1


console.log(newarr) ;
  [Array(2)]0: Array(2)0: "Troubles de la vessie"1: "Troubles de l'érection"length: 2__proto__: Array(0)length: 1__proto__: Array(0)     

But i need this kind of array :
(2) ["a", "b"] 0:"a" 1:"b"

4
  • 1
    First one is 2 dimensional array i.e. array of arrays. To get the first value you can simply use array[0] Commented Aug 28, 2018 at 11:40
  • 2
    The first Array really doesn't have anything in common with the second Array, it's really hard to tell how you want the first array transformed Commented Aug 28, 2018 at 11:41
  • Please update your question: What is difference between arrays is not really specific. I see multiple formats for your arrays, could you change that? And lastly this question has not really to do anything with Angular (CLI), perhaps it might be better to leave that out. Please refer to: How to ask questions. Commented Aug 28, 2018 at 11:44
  • I updated my question. thanks Commented Aug 28, 2018 at 11:46

1 Answer 1

1

The first array seems to be something like [["a", "b"],,,,] , probably a sparse array, but we are sure that the first position is another array (dense) made of 2 elements. To transform that into ["a", "b"] you could use Spread syntax like this:

var arr = [["a", "b"],,,,];

var newArr = [...arr[0]]

console.log(newArr)

in your code: newArr = [...this.ppssToDisplay.effetsind[0]];

Sign up to request clarification or add additional context in comments.

5 Comments

I obtain this : [Array(2)] 0:(2) ["Troubles de la vessie", "Troubles de l'érection"] length:1 proto:Array(0)
and to this. ppssToDisplay i have : [Array(1)] 0 : Array(1) 0 : (2) ["Troubles de la vessie", "Troubles de l'érection"]
It's hard doctor ?
@Emmeus lol now newA =" T" but console.log(this.ppsToDisplay.effetsind) say : (2) ["Troubles de la vessie", "Troubles de l'érection"]
@Newbiiiie I'm confused now this.ppsToDisplay.effetsind then is the right

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.