Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the next element on Map function?

I'm trying to make some coordinate plane functions on React native. But I'm having a problem that I don't know how to get the next element on my array.

This is my array:

[
{"M":["0","0"]},
{"H":"100"},
{"V":"0"},
{"H":"100"},
{"V":"100"},
{"H":"0"},
{"V":"100"},
{"H":"0"},
{"V":"0"},
]

This is my function:

const rotate = (pathArray, angle) =>{
    if (angle > 0){
        let vCordinate;
        return pathArray.map((cordinate)=>{
            if(Object.entries(cordinate)[0][0] == "M"){
                let mCordinate = Object.entries(cordinate)[0][1];
                mCordinate[0] = (parseInt(mCordinate[0]) * Math.cos(angle)) - (parseInt(mCordinate[1]) * Math.sin(angle));
                mCordinate[1] = (parseInt(mCordinate[1]) * Math.cos(angle)) + (parseInt(mCordinate[0]) * Math.sin(angle));
                return {[Object.entries(cordinate)[0][0]]: mCordinate};
            }
            //LOGIC TO GET NEXT ELEMENT
            if(Object.entries(cordinate)[0][0] == "H"){
                let hCordinate = Object.entries(cordinate)[0][1];
                vCordinate = Object.entries(cordinate)[0][1]
                return {[Object.entries(cordinate)[0][0]]: vCordinate};
            }
            if(Object.entries(cordinate)[0][0] == "V"){
                return {[Object.entries(cordinate)[0][0]]: vCordinate};
            }
        })
    }
    return pathArray;
}

In this point I need to get the next element when I've hit "H".

How to do this?

like image 412
Raul Blosfeld Wohnrath Avatar asked Mar 24 '18 08:03

Raul Blosfeld Wohnrath


People also ask

Does map return a new array?

The map() method returns an entirely new array with transformed elements and the same amount of data. In the case of forEach() , even if it returns undefined , it will mutate the original array with the callback .

Can we break a map function?

To break a map() loop in React: Call the slice() method on the array to get a portion of the array. Call the map() method on the portion of the array. Iterate over the portion of the array.

How do you use the map function?

Definition and Usagemap() creates a new array from calling a function for every array element. map() calls a function once for each element in an array. map() does not execute the function for empty elements. map() does not change the original array.


Video Answer


1 Answers

The callback of map method accepts 3 arguments:

  • current item value;
  • current item index;
  • the array map was called upon.

So, you could use index to get next element value:

var newArray  = myArray.map(function(value, index, elements) {
  var next = elements[index+1];
  // do something
});

or

var newArray  = myArray.map(function(value, index) {
  var next = myArray[index+1];
  // do something
});

Note, the value of next variable may be undefined. Test the variable, if need, to prevent errors.

like image 66
Alexander Avatar answered Oct 16 '22 22:10

Alexander