dr Code
dr Code

Reputation: 225

Transform arrays and reverse

Here I have a code that will transform me array on X and Y format:

So my code:

var tacke = response.routes[0].overview_path,
    newTacke = [[]];
for(var i = 0, len = overviewPath.length; i < len; i++) {
    newTacke[0].push({
       X: tacke[i].lat(),
       Y: tacke[i].lng()
    });
}

will transform array tacke into newArray newTacke from format:

TACKE format

[
  [56.789, 12.34],
  [123.45, 87.65]
]

to format:

newTACKE format

[
  [
    {
      X: 12.34
      Y: 56.789
    },
    {
      X: 87.65,
      Y: 123.45
    }
  ]
]

So now I need an solution to tranform if I have newTacke into old format like tacke

So I did it to tranform var tacke = response.routes[0].overview_path, into new format,

Now I need to back it in tacke format so to be like this:

  [
          [56.789, 12.34],
          [123.45, 87.65]
        ]

Is there any way to do that?

So now just newTacke to make back in tacke array format

Upvotes: 1

Views: 179

Answers (4)

Alireza Masali
Alireza Masali

Reputation: 688

this will make an JsonArray from a class that named Points for all of it's properties

Potints= function (x,y){
this.X=x;
this.Y=y;
this.MakeJson=function() {
return JSON.stringify(this);
}
}
var JsonArry=new Array();
for(var i = 0, len = overviewPath.length; i < len; i++) {
  ObjPoints=new Potints(tacke[i].lat(),tacke[i].lng());
  JsonArry.push(ObjPoints.MakeJson());
    });
}

Upvotes: 2

Mohamed Ali JAMAOUI
Mohamed Ali JAMAOUI

Reputation: 14689

given the newTackle format:

 newTackle = [
  [
    {
      X: 12.34,
      Y: 56.789
    },
    {
      X: 87.65,
      Y: 123.45
    }
  ]
];

you can get back to the old format this way:

tackle  = newTackle[0].map(function(k){
    return [k.Y, k.X];
});

and the result:

 console.log(tackle)
 // [
 //        [56.789, 12.34],
 //        [123.45, 87.65]
 // ]

Upvotes: 2

Psych Half
Psych Half

Reputation: 1411

your question is confusing to me.. anyways.. if your newTacke is like the below format.. this will work..

newTacke =  [[{X: 12.34, Y: 56.789 }, {X: 87.65, Y: 123.45 }]] ;

oldTacke = [] ;

newTacke.forEach(function (el,i,arr) {
  oldTacke.push([el[0].X, el[0].Y] , [el[1].X, el[1].Y] ) ;
});
console.log(oldTacke); // [[12.34, 56.789], [87.65, 123.45]]

if this is not what you expect..please clarify your answer..

Upvotes: 1

LSerni
LSerni

Reputation: 57408

Looks to me as if you can just access first member of newTacke, getting an array. Then you iterate that array getting the various couples:

// pseudo code

oldTacke = []
for pair in newTacke[0]   # we iterate on [{X Y} {X Y} {X Y}]:
    # pair is now { X Y }
    oldTacke.append([ pair.X, pair.Y ])

Upvotes: 1

Related Questions