0

I have to order this

("Forecast":["WP17 - CO746U100 - Wenceslao Escalante --- Telefonia - Implementaciones","WP19 - CO804U100 - Viamonte Nuevo --- Telefonia - Implementaciones","WP18 - CO663U200 - Corral de Bustos 2 --- Telefonia - Implementaciones","WP22 - MBA205 - Olavarria 2 --- Telefonia - Integraciones","WP23 - MC1165 - Martinez 4 --- Telefonia - Integraciones","WP26 - MC1881 - Bajo Flores 2 --- Telefonia - Integraciones","WP12 - FD454 - Shopping Mendoza --- Telefonia - Implementaciones","WP65 - TEST1 - TEST-TASK --- Telefonia - Integraciones","WP25 - MC1510 - Nunez 4 Av Congreso --- Telefonia - Integraciones","WP1 - UFO555 - Villa Maria --- Telefonia - Implementaciones","WP28 - MFO066 - CAC Formosa --- Telefonia - Integraciones","WP70 - 4312341 - sarasa --- Telefonia - Integraciones","WP71 - 54325 - sarasa2 --- Telefonia - Integraciones","WP75 - 00001 - Complejo Capitalinas --- Obras Civiles","WP76 - 00002 - Mejora de la Ca\u00f1ada --- Obras Civiles","WP32 - MCO800 - Unquillo Norte Nuevo --- Telefonia - Implementaciones","WP33 - MCO817 - Hospital Privado Nuevo --- Telefonia - Implementaciones","WP34 - LTF039 - Ushuaia Paseo del Fuego --- Telefonia - Implementaciones","WP36 - LSJ062 - San Juan UMTS 2 --- Telefonia - Implementaciones","WP37 - LCO261 - ZIPOLI NUEVO --- Telefonia - Implementaciones","WP63 - 7878 - SIAM SA --- Telefonia - Implementaciones","WP64 - MCF162 - Kansas Acassuso Test --- Telefonia - Implementaciones","WP69 - 11111 - Nueva Tare --- Telefonia - Implementaciones"],) 

As it says there Implementaciones, integraciones and Obras.

Code:

  let ObrasV = []
  let ImplementacionesV = []
  let IntegracionesV = []

  if(this.state.dataSource){
    this.state.dataSource.forEach(function(DATA) {
      if(/^Integraciones/.test(DATA)) {
        IntegracionesV = IntegracionesV.concat(DATA)      
      }
      if(/^Implementaciones/.test(DATA)) {
        ImplementacionesV = ImplementacionesV.concat(DATA)
      }
      if(/^Obras/.test(DATA)) {
        ObrasV = ObrasV.concat(DATA)
      }
    })
    this.setState({
      Integraciones: IntegracionesV,
      Implementaciones: ImplementacionesV,
      Obras: ObrasV,
    })
    } 

I am new to react native as I can do it?

3
  • i can tell u’re argentinian lol... sort it how? alphabetically?? u can use plain javascript for sorting Commented Jan 21, 2019 at 14:39
  • I have to order it in 3 lists for example: Implentaciones: [ ] integraciones: ["WP17 - CO746U100 - Wenceslao Escalante --- Telefonia - Implementaciones", "WP19 - CO804U100 - Viamonte Nuevo --- Telefonia - Implementaciones", etc ] Obras: ["WP75 - 00001 - Complejo Capitalinas --- Obras Civiles", etc] Commented Jan 21, 2019 at 14:46
  • below he gave u a really good example on how to iterate and filter with javascript. Commented Jan 21, 2019 at 14:48

2 Answers 2

1

You could use a mix of reduce, filter and includes to achieve it

const data = ["WP17 - CO746U100 - Wenceslao Escalante --- Telefonia - Implementaciones", "WP19 - CO804U100 - Viamonte Nuevo --- Telefonia - Implementaciones", "WP18 - CO663U200 - Corral de Bustos 2 --- Telefonia - Implementaciones", "WP22 - MBA205 - Olavarria 2 --- Telefonia - Integraciones", "WP23 - MC1165 - Martinez 4 --- Telefonia - Integraciones", "WP26 - MC1881 - Bajo Flores 2 --- Telefonia - Integraciones", "WP12 - FD454 - Shopping Mendoza --- Telefonia - Implementaciones", "WP65 - TEST1 - TEST-TASK --- Telefonia - Integraciones", "WP25 - MC1510 - Nunez 4 Av Congreso --- Telefonia - Integraciones", "WP1 - UFO555 - Villa Maria --- Telefonia - Implementaciones", "WP28 - MFO066 - CAC Formosa --- Telefonia - Integraciones", "WP70 - 4312341 - sarasa --- Telefonia - Integraciones", "WP71 - 54325 - sarasa2 --- Telefonia - Integraciones", "WP75 - 00001 - Complejo Capitalinas --- Obras Civiles", "WP76 - 00002 - Mejora de la Ca\u00f1ada --- Obras Civiles", "WP32 - MCO800 - Unquillo Norte Nuevo --- Telefonia - Implementaciones", "WP33 - MCO817 - Hospital Privado Nuevo --- Telefonia - Implementaciones", "WP34 - LTF039 - Ushuaia Paseo del Fuego --- Telefonia - Implementaciones", "WP36 - LSJ062 - San Juan UMTS 2 --- Telefonia - Implementaciones", "WP37 - LCO261 - ZIPOLI NUEVO --- Telefonia - Implementaciones", "WP63 - 7878 - SIAM SA --- Telefonia - Implementaciones", "WP64 - MCF162 - Kansas Acassuso Test --- Telefonia - Implementaciones", "WP69 - 11111 - Nueva Tare --- Telefonia - Implementaciones"]

const result = ['Implementaciones', 'Integraciones', 'Obras']
    .reduce((total, category) => ({
        ...total,
        [category]: data.filter(line => line.includes(category))
    }), {})

console.log(result)

The resulting object will contain 3 arrays containing all your data

An easier to read implementation using forEach :

const data = ["WP17 - CO746U100 - Wenceslao Escalante --- Telefonia - Implementaciones", "WP19 - CO804U100 - Viamonte Nuevo --- Telefonia - Implementaciones", "WP18 - CO663U200 - Corral de Bustos 2 --- Telefonia - Implementaciones", "WP22 - MBA205 - Olavarria 2 --- Telefonia - Integraciones", "WP23 - MC1165 - Martinez 4 --- Telefonia - Integraciones", "WP26 - MC1881 - Bajo Flores 2 --- Telefonia - Integraciones", "WP12 - FD454 - Shopping Mendoza --- Telefonia - Implementaciones", "WP65 - TEST1 - TEST-TASK --- Telefonia - Integraciones", "WP25 - MC1510 - Nunez 4 Av Congreso --- Telefonia - Integraciones", "WP1 - UFO555 - Villa Maria --- Telefonia - Implementaciones", "WP28 - MFO066 - CAC Formosa --- Telefonia - Integraciones", "WP70 - 4312341 - sarasa --- Telefonia - Integraciones", "WP71 - 54325 - sarasa2 --- Telefonia - Integraciones", "WP75 - 00001 - Complejo Capitalinas --- Obras Civiles", "WP76 - 00002 - Mejora de la Ca\u00f1ada --- Obras Civiles", "WP32 - MCO800 - Unquillo Norte Nuevo --- Telefonia - Implementaciones", "WP33 - MCO817 - Hospital Privado Nuevo --- Telefonia - Implementaciones", "WP34 - LTF039 - Ushuaia Paseo del Fuego --- Telefonia - Implementaciones", "WP36 - LSJ062 - San Juan UMTS 2 --- Telefonia - Implementaciones", "WP37 - LCO261 - ZIPOLI NUEVO --- Telefonia - Implementaciones", "WP63 - 7878 - SIAM SA --- Telefonia - Implementaciones", "WP64 - MCF162 - Kansas Acassuso Test --- Telefonia - Implementaciones", "WP69 - 11111 - Nueva Tare --- Telefonia - Implementaciones"]

const result = {};

['Implementaciones', 'Integraciones', 'Obras'].forEach(category => {
    result[category] = data.filter(line => line.includes(category))
})

console.log(result)

Sign up to request clarification or add additional context in comments.

Comments

0

Simply notice the new RegExp() and avoid using ^

let ObrasV = []
  let ImplementacionesV = []
  let IntegracionesV = []

  if(this.state.dataSource){
    this.state.dataSource.forEach(function(DATA) {
      if(new RegExp(/Integraciones/).test(DATA)) {
        IntegracionesV = IntegracionesV.concat(DATA)      
      }
      if(new RegExp(/Implementaciones/).test(DATA)) {
        ImplementacionesV = ImplementacionesV.concat(DATA)
      }
      if(new RegExp(/Obras/).test(DATA)) {
        ObrasV = ObrasV.concat(DATA)
      }
    })
    this.setState({
      Integraciones: IntegracionesV,
      Implementaciones: ImplementacionesV,
      Obras: ObrasV,
    })
    } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.