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

155
Vistas
replace an object in an array without convert array to string javascript app script

I'm using app script to get information from API and use it as JSON.

var data = UrlFetchApp.fetch(***);
var obj = JSON.parse(data); // To JSON
Logger.log(typeof(obj)); // return type as object

example of return array JSON:

var dataJson = [{age=20, gender=female, country=usa, name=sali, type=female},{age=25, gender=male, country=usa, name=john, type=female},{age=19, gender=female, country=usa, name=anita, type=female},{age=22, gender=male, country=usa, name=fredo, type=female}]

I want if gender = female change country to uk. and if gender = male change country to canada.

I want after update array keep type as object not as string.

result as:

[{age=20, gender=female, country=uk, name=sali, type=female},{age=25, gender=male, country=canada, name=john, type=female},{age=19, gender=female, country=uk, name=anita, type=female},{age=22, gender=male, country=canada, name=fredo, type=female}]

note: the data in not fixed is changed dynamic.

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

0

You can simply map over it

var dataJson = [{age:20, gender:'female', country:'usa', name:'sali', type:'female'},{age:25, gender:'male', country:'usa', name:'john', type:'male'}]


const result = dataJson.map(({gender, ...rest}) => {
const isFemale = gender === 'female'
 return {
   ...rest,
   gender: isFemale? 'woman': 'man',
   country: isFemale ?'uk': 'canada'
 }
})

console.log(result)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Are you simply looking for:

dataJson = dataJson.map(elem => {
    if(elem.gender === 'female'){
        elem.country = 'uk'
    }
    if(elem.gender === 'male'){
        elem.country = 'canada'
    }
    return elem
})
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