Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

183
Vistas
javascript parse string and check with conditional logic

If I have data that looks like this

const reqObjects = {
    "VAV1": "read 12345:2 analogInput 2",
    "VAV2": "read 12345:3 analogInput 1",
    "VAV3": "read 12345:4 analogInput 1",
    "VAV4": "read 12345:5 analogInput 2",
    "VAV5": "read 12345:6 analogInput 1",
    "VAV6": "read 12345:7 analogInput 2",
    "VAV7": "read 12345:8 analogInput 1",
    "VAV8": "read 12345:9 analogInput 1",
    "VAV9": "read 12345:10 analogInput 2",
    "VAV10": "read 12345:11 analogInput 1"
  }

How could I loop through this by feeding this data into a function, parse keys into string values, and use conditional logic to verify each of key values seem appropriate?

Dumbing this down a little bit for one string:

var myVar = "read 12345:2 analogInput 2";

I can split this into pieces:

var myVarParts = myVar.split(" ");

And assign variables to each piece:

requestType = myVarParts[0];
console.log(requestType)

deviceAddress = myVarParts[1];
console.log(deviceAddress)

pointType = myVarParts[2];
console.log(pointType)

pointAddress = myVarParts[3];
console.log(pointAddress)

Trying to make up some conditional logic if the requestType is good if its a read, write, release:

if(requestType === "read"){
    console.log("Good on read");
  }else if(requestType === "write"){
    console.log("Good on write");
  }else if(requestType === "release"){
    console.log("Good on release");
  }else{
    console.log("BAD requestType");
  }

The pointType is good if its a analogInput, analogValue, binaryValue, binaryInput.

Could someone give me a tip on what a Boolean JavaScript function would look like that could loop through this data and verify the keys look OK?

const reqObjects = {
    "VAV1": "read 12345:2 analogInput 2",
    "VAV2": "read 12345:3 analogInput 1",
    "VAV3": "read 12345:4 analogInput 1",
    "VAV4": "read 12345:5 analogInput 2",
    "VAV5": "read 12345:6 analogInput 1",
    "VAV6": "read 12345:7 analogInput 2",
    "VAV7": "read 12345:8 analogInput 1",
    "VAV8": "read 12345:9 analogInput 1",
    "VAV9": "read 12345:10 analogInput 2",
    "VAV10": "read 12345:11 analogInput 1"
  }

Thank you I am trying to learn JavaScript.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You could use the regex ^(read|write|release)\s(\d+:\d+)\s(analog|binary)(Input|Value)\s\d$ to match the pattern.


  • (read|write|release) are the options for the request type
  • (\d+:\d+) makes sure the device address is made up of numbers separated by a colon
  • (analog|binary)(Input|Value) are the options for the point type
  • \d matches the single digit at the end

You can use Object.values and run <Array>.every to loop through the values and make sure they all match the pattern.

const regex = /^(read|write|release)\s(\d+:\d+)\s(analog|binary)(Input|Value)\s\d$/;

const reqObjects = {
  "VAV1": "read 12345:2 analogInput 2",
  "VAV2": "read 12345:3 analogInput 1",
  "VAV3": "read 12345:4 analogInput 1",
  "VAV4": "read 12345:5 analogInput 2",
  "VAV5": "read 12345:6 analogInput 1",
  "VAV6": "read 12345:7 analogInput 2",
  "VAV7": "read 12345:8 analogInput 1",
  "VAV8": "read 12345:9 analogInput 1",
  "VAV9": "read 12345:10 analogInput 2",
  "VAV10": "read 12345:11 analogInput 1"
}

console.log(Object.values(reqObjects).every(val => val.match(regex)))

about 4 years ago · Juan Pablo Isaza Denunciar

0

  • Using Object#values, get the list of values from the object.
  • Using Array#every, iterate over the above to validate each item, you can modify the validation helpers if needed.

const reqObjects = {
  "VAV1": "read 12345:2 analogInput 2",
  "VAV2": "read 12345:3 analogInput 1",
  "VAV3": "read 12345:4 analogInput 1",
  "VAV4": "read 12345:5 analogInput 2",
  "VAV5": "read 12345:6 analogInput 1",
  "VAV6": "read 12345:7 analogInput 2",
  "VAV7": "read 12345:8 analogInput 1",
  "VAV8": "read 12345:9 analogInput 1",
  "VAV9": "read 12345:10 analogInput 2",
  "VAV10": "read 12345:11 analogInput 1"
};

const validateRequestType = type => 
  ['read', 'write', 'release'].includes(type);
const validatePointType = type => 
  ['analogInput', 'analogValue', 'binaryValue', 'binaryInput'].includes(type);
const validate = str => {
  const [requestType, deviceAddress, pointType, pointAddress] = str.split(' ');
  return validateRequestType(requestType) && validatePointType(pointType);
} 
const valid = Object.values(reqObjects).every(validate);

console.log(valid);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda