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

144
Vistas
Covert key-value javascript object into array format

I am trying to access java script object which is available in the below format:

[
  {'To':'A','From':'X','Weight':5},
  {'To':'A','From':'Y','Weight':7},
  {'To':'A','From':'Z','Weight':6},
  {'To':'B','From':'X','Weight':2},
  {'To':'B','From':'Y','Weight':9},
  {'To':'B','From':'Z','Weight':4}
]

How can I access above object to create array like below ?

[
  [ 'A', 'X', 5 ],
  [ 'A', 'Y', 7 ],
  [ 'A', 'Z', 6 ],
  [ 'B', 'X', 2 ],
  [ 'B', 'Y', 9 ],
  [ 'B', 'Z', 4 ]
]
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Well, you can use array method .map()

let arr = [
  {'To':'A','From':'Y','Weight':7},
  {'To':'A','From':'Z','Weight':6}
]

let result = arr.map(Object.values)

console.log(result)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can

  • Use Array.map() and Object.values():

    const arrOfArrs = arr.map( Object.values );
    
  • Use Lodash's _.map() and _.values() in the same manner:

    const _ = require('lodash');
    . . .
    const arrOfArrs = _.map( arr , _.values );
    
    

You should, however, be aware that the order in which an object's properties are iterated over (and hence returned) is not guaranteed in any way by the Ecmascript/Javascript standard. It can vary from Javascript implementation to implementation, and can even change from execution to execution. The most common order in which things are returned is insertion order, but that's not guaranteed.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use a map function:

const l = [
  {'To':'A','From':'X','Weight':5},
  {'To':'A','From':'Y','Weight':7},
  {'To':'A','From':'Z','Weight':6},
  {'To':'B','From':'X','Weight':2},
  {'To':'B','From':'Y','Weight':9},
  {'To':'B','From':'Z','Weight':4}
]

const newArray = l.map(el => [el.To, el.From, el.Weight])
console.log(newArray);

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