TransWikia.com

Obtener valores de un JSON usando destructuring

Stack Overflow en español Asked on December 22, 2021

Estoy aprendiendo destructuring en JavaScript y quiero obtener del json el NOMBRE_UGL , DIRECCION , LOCALIDAD de attributes y de geometry x e y me podrían ayudar por favor. Gracias!

features: [
{
attributes: {
OBJECTID: 1,
ID_UGL: "1",
NOMBRE_UGL: "TUCUMAN",
ID_AGENCIA: "10000",
NOMBRE_AGENCIA: "UGL I - TUCUMAN",
DIRECCION: "SANTIAGO DEL ESTERO 1020 ",
LOCALIDAD: "SAN MIGUEL DE TUCUMAN",
X: -65.21076584,
Y: -26.82272171
},
geometry: {
x: -65.21076583687207,
y: -26.822721714275644
}
}
]

2 Answers

Puedes usar destructores simples o anidados, te dejo los siguientes ejemplos incluyendo el uso de los destructores para arreglos.

Tenemos la primera opción, en la cual usaremos lo que llamaré "destructores simples".

const json = {
  features: [
    {
      attributes: {
        OBJECTID: 1,
        ID_UGL: '1',
        NOMBRE_UGL: 'TUCUMAN',
        ID_AGENCIA: '10000',
        NOMBRE_AGENCIA: 'UGL I - TUCUMAN',
        DIRECCION: 'SANTIAGO DEL ESTERO 1020 ',
        LOCALIDAD: 'SAN MIGUEL DE TUCUMAN',
        X: -65.21076584,
        Y: -26.82272171,
      },
      geometry: {
        x: -65.21076583687207,
        y: -26.822721714275644,
      },
    },
  ],
};

// Destructor de arreglos (Obtenemos la posición 0 en variable feature)
const [feature] = json.features;

// Destructor de objetos (Obtenemos propiedad NOMBRE_UGL dentro de attributes)
const { NOMBRE_UGL, DIRECCION, LOCALIDAD } = feature.attributes;
console.log('NOMBRE_UGL: ' + NOMBRE_UGL);
console.log('DIRECCION: ' + DIRECCION);
console.log('LOCALIDAD: ' + LOCALIDAD);

// Destructor de objetos (Obtenemos X y Y de geometry)
const { x, y } = feature.geometry;
console.log('x: ' + x);
console.log('y: ' + y);

Y la segunda opción que llamaré "destructores anidados".

const json = {
  features: [
    {
      attributes: {
        OBJECTID: 1,
        ID_UGL: '1',
        NOMBRE_UGL: 'TUCUMAN',
        ID_AGENCIA: '10000',
        NOMBRE_AGENCIA: 'UGL I - TUCUMAN',
        DIRECCION: 'SANTIAGO DEL ESTERO 1020 ',
        LOCALIDAD: 'SAN MIGUEL DE TUCUMAN',
        X: -65.21076584,
        Y: -26.82272171,
      },
      geometry: {
        x: -65.21076583687207,
        y: -26.822721714275644,
      },
    },
  ],
};

// Destructor de arreglos (Obtenemos la posición 0 en variable feature)
const [feature] = json.features;

// Destructor anidado de objetos (Obtenemos las propiedades de attributes y geometry en una sola expresión)
const { attributes: { NOMBRE_UGL, DIRECCION, LOCALIDAD }, geometry: { x, y } } = feature;
console.log('NOMBRE_UGL: ' + NOMBRE_UGL);
console.log('DIRECCION: ' + DIRECCION);
console.log('LOCALIDAD: ' + LOCALIDAD);
console.log('x: ' + x);
console.log('y: ' + y);

Nota: La segunda opción puede parecer un tanto llamativa al poder hacer todo en una sola línea, pero en situaciones complejas puede no ser viable por cuestiones de legibilidad.

Answered by Jose Noriega on December 22, 2021

Tienes un objeto cuya propiedad features es un arreglo. Te dejo un ejemplo de como accesar la propiedad NOMBRE_UGL:

var json = {
features: [
{
attributes: {
OBJECTID: 1,
ID_UGL: "1",
NOMBRE_UGL: "TUCUMAN",
ID_AGENCIA: "10000",
NOMBRE_AGENCIA: "UGL I - TUCUMAN",
DIRECCION: "SANTIAGO DEL ESTERO 1020 ",
LOCALIDAD: "SAN MIGUEL DE TUCUMAN",
X: -65.21076584,
Y: -26.82272171
},
geometry: {
x: -65.21076583687207,
y: -26.822721714275644
}
}
]
};

console.log(json.features[0].attributes.NOMBRE_UGL);

Answered by alanfcm on December 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP