admin管理员组

文章数量:1186974

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?

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?

Share Improve this question edited Mar 24, 2018 at 8:36 user202729 3,9353 gold badges29 silver badges45 bronze badges asked Mar 24, 2018 at 8:32 Raul Blosfeld WohnrathRaul Blosfeld Wohnrath 1911 gold badge3 silver badges13 bronze badges 3
  • Possible duplicate. – user202729 Commented Mar 24, 2018 at 8:37
  • Use the second argument of the map function which represent index, when you find 'H' and then access index+1 – Hassan Imam Commented Mar 24, 2018 at 8:39
  • 1 Possible duplicate of When mapping through an array check next item property – user202729 Commented Mar 24, 2018 at 8:51
Add a comment  | 

1 Answer 1

Reset to default 27

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.

本文标签: javascriptHow to get the next element on Map functionStack Overflow