특정 값을 가진 배열이 있고 계산을 수행하고 있습니다.
다음은 내 배열이 결과를 확인하는 것입니다.
var arr2 = [1,2,2,3]
var newArr = [["a",0,1,2,0,223],["b",1,0,0,0,0],["c",0,0,0,0,223],["d",0,1,2,0,223]]
const result = newArr.reduce((results, element, index) => {
return [
...results, // push the rest of the results onto the new array
[
...element, // keep all the current items of the element (the 1d array)
Math.round(element[element.length-1]-(element[element.length-1]/(element[element.length-3]+element[element.length-4])*arr2[index])) // add new element, by querying the last element of the array and the appropriate index of the second array
]
]
},[]);
console.log(result)
내 제수가 0이 될 때까지 잘 작동합니다. 위의 코드에서는 (element[element.length-3]+element[element.length-4])
때때로 0이 나옵니다. 이것은 배열에서 NaN 또는 무한대를 제공하며 대신 0을 던질 수 있습니다.
if condition 내부에 사용할 수있는 방법이 있나요? 아니면 다른 해결책이 있습니까?
0 값을 false로 처리 할 수 있습니다. 이 경우 제수를 1로 설정할 수 있습니다.
((요소 [요소. 길이 -3] + 요소 [요소. 길이 -4]) || 1)
다른 옵션은 별도의 변수에서 계산을 수행하는 것입니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다