“Convertir la matriz a objeto en JavaScript” Código de respuesta

matriz js en objeto

const names = ['Alex', 'Bob', 'Johny', 'Atta'];

// convert array to th object
const obj = Object.assign({}, names);

// print object
console.log(obj);

// {0: "Alex", 1: "Bob", 2: "Johny", 3: "Atta"}
Plain Pygmy

Convertir la matriz a objeto en JavaScript

const array =    [ [ 'cardType', 'iDEBIT' ],
      [ 'txnAmount', '17.64' ],
      [ 'txnId', '20181' ],
      [ 'txnType', 'Purchase' ],
      [ 'txnDate', '2015/08/13 21:50:04' ],
      [ 'respCode', '0' ],
      [ 'isoCode', '0' ],
      [ 'authCode', '' ],
      [ 'acquirerInvoice', '0' ],
      [ 'message', '' ],
      [ 'isComplete', 'true' ],
      [ 'isTimeout', 'false' ] ];
      
const obj = Object.fromEntries(array);
console.log(obj);
Naughty Nightingale

Convertir la matriz a objeto JavaScript

const convertArrayToObject = (array, key) =>
  array.reduce(
    (obj, item) => ({
      ...obj,
      [item[key]]: item
    }),
    {}
  );
Moise Mbakop

matriz a objeto

// array to object 
let data = [
  {id: 1, country: 'Germany', population: 83623528},
  {id: 2, country: 'Austria', population: 8975552},
  {id: 3, country: 'Switzerland', population: 8616571}
];

let dictionary = Object.assign({}, ...data.map((x) => ({[x.id]: x.country})));
// {1: "Germany", 2: "Austria", 3: "Switzerland"}
Fine Falcon

JavaScript Convertir la matriz a objeto

function arrayToObject(arr) {
    var obj = {};
    for (var i = 0; i < arr.length; ++i){
        obj[i] = arr[i];
    }
    return obj;
}
var colors=["red","blue","green"];
var colorsObj=arrayToObject(colors);//{0: "red", 1: "blue", 2: "green"}
Grepper

ES6 Convertir la matriz a objeto

const result = arr.reduce((obj, cur) => ({...obj, [cur.sid]: cur}), {})
victormoove

Respuestas similares a “Convertir la matriz a objeto en JavaScript”

Preguntas similares a “Convertir la matriz a objeto en JavaScript”

Más respuestas relacionadas con “Convertir la matriz a objeto en JavaScript” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código