Ссылка на ту же позицию значения в предыдущем массиве в массиве массивов / многомерном массиве - PullRequest
0 голосов
/ 25 сентября 2019

В настоящее время я работаю с 2 функциями, одна возвращает массив массивов (выглядит следующим образом [[63,2], [74,3]]

В настоящее время у меня есть это:

let thermostatTempArray = [71, 72, 63, 74, 75, 76, 77, 78, 79, 80, 79, 78, 77]
    // index 2,3,4,5 = 63,74,75,76 === these indexes should return 
    // index 10,11,12 = 79,78,77 === these indexes should not return when successful
    let coolingCallsArray = [2, 3, 4, 5, 10, 11, 12,]

    function findValuesByIndex(indexGivenArray, valueGivenArray) {
      const matchedArray = []
      valueGivenArray.forEach(function (valueGiven,index) {
        const indexGiven = indexGivenArray[index]
        if (indexGiven !== undefined) {
          const matchingValue = valueGivenArray[indexGiven]
          matchedArray.push([matchingValue, indexGiven])
        }
      })
      return matchedArray
    }

    const coolingVals = findValuesByIndex(coolingCallsArray, thermostatTempArray)



    function findOverheatingTerminals(matchedArray) {
      overheatingTerminalsArray = []
      matchedArray.forEach(function(match,index) {
        const [matchingValue,matchedIndex] = match
          console.log(`Value: ${matchingValue} Index: ${matchedIndex}`)
          const prevMatchingVal = match[[0]]
          console.log(prevMatchingVal)
      } )
    }

    const overHeatingTerminals = findOverheatingTerminals(coolingVals)

    console.log(overHeatingTerminals)

То, что я хочу сделать, это сравнить предыдущее совпадающее значение (что может выглядеть как matchingValue [index-1], если оно было в том же массиве) с текущим значением (т.е. сравнить 74 с 63) - ТОЛЬКОесли второе число в массиве является последовательным (если оно было [[63,2], [74,4] не сравнивать), я просто не уверен, как правильно получить переменную, чтобы я мог даже ссылаться на предыдущие значения.

1 Ответ

0 голосов
/ 25 сентября 2019

В итоге я поднялся на массив (к предыдущему индексу для соответствующих массивов) и разобрал его следующим образом:

function findOverheatingTerminals(matchedArray) {
  const overheatingTerminalsArray = []
  matchedArray.forEach(function (match, index) {
    const [matchingValue, matchedIndex] = match
    const prevMatchingArr = matchedArray[index - 1]
    if (prevMatchingArr !== undefined) {
      const [prevMatchingval, prevMatchindex] = prevMatchingArr
      if (matchingValue > prevMatchingval && matchedIndex === prevMatchindex + 1) {
        overheatingTerminalsArray.push(matchedIndex)
      }
    }
  })
  return overheatingTerminalsArray
}

exports.findOverheatingTerminals = findOverheatingTerminals
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...