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

90
Vistas
How do i create a function that will change the property inside an object that is located inside of an array, in a JSON

so ive been currently stuck on a JavaScript assignment for hours now.

so ive been given a Json file with the following data :

{
    "owner": "Jun31d",
    "info": [{ "id": 1, "name": "bob", "flavour": "vanilla", "age": 43 },
             { "id": 2, "name": "Juneid", "flavour": "Chocolate", "age": 19 },
             { "id": 3, "name": "Patrick", "flavour": "Strawberry", "age": 25 },
             { "id": 4, "name": "Jean", "flavour": "Mint", "age": 31 },
             { "id": 5, "name": "Ahmad", "flavour": "vanilla", "age": 18 },
             { "id": 6, "name": "Ali", "flavour": "Bubblegum", "age": 19 },
             { "id": 7, "name": "Frank", "flavour": "Pistachio", "age": 23 }
            ]
}

The instruction for my assigment is the following :

Instruction

So from what ive understood i need to create a function that holds two string parameters, And ill need to change the property inside of my object that is located in an array to a new property.

And until now heres what i did :

'use strict'

const iceCream = require('./main.json')

let namespace = {

    changeProp : function (newprop,oldprop) {
        for (let oldprop in iceCream.info) {
           
        }
    }


}

I know it is not much at all, but i just want some help to know how can i move forward a little bit more on my assigment.

Any help is appreciated, thank you

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

0

You can easily replace the oldKey with newKey using map, Object.keys, and reduce

const data = {
  owner: "Jun31d",
  info: [
    { id: 1, name: "bob", flavour: "vanilla", age: 43 },
    { id: 2, name: "Juneid", flavour: "Chocolate", age: 19 },
    { id: 3, name: "Patrick", flavour: "Strawberry", age: 25 },
    { id: 4, name: "Jean", flavour: "Mint", age: 31 },
    { id: 5, name: "Ahmad", flavour: "vanilla", age: 18 },
    { id: 6, name: "Ali", flavour: "Bubblegum", age: 19 },
    { id: 7, name: "Frank", flavour: "Pistachio", age: 23 },
  ],
};

function changePropertyName(arr, oldName, newName) {
  return arr.map((o) => {
    return Object.keys(o).reduce((acc, curr) => {
      curr === oldName ? (acc[newName] = o[oldName]) : (acc[curr] = o[curr]);
      return acc;
    }, {});
  });
}

console.log(changePropertyName(data.info, "flavour", "bestFlavour"));
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

This is how you can do it. Easy, simple and nasty solution.

const data = {
  owner: 'Jun31d',
  info: [
    { id: 1, name: 'bob', flavour: 'vanilla', age: 43 },
    { id: 2, name: 'Juneid', flavour: 'Chocolate', age: 19 },
    { id: 3, name: 'Patrick', flavour: 'Strawberry', age: 25 },
    { id: 4, name: 'Jean', flavour: 'Mint', age: 31 },
    { id: 5, name: 'Ahmad', flavour: 'vanilla', age: 18 },
    { id: 6, name: 'Ali', flavour: 'Bubblegum', age: 19 },
    { id: 7, name: 'Frank', flavour: 'Pistachio', age: 23 }
  ]
};

const renameProperty = (a, e, n) =>
    a.map((el) => {
        el[n] = el[e];
        delete el[e];
        return el;
    });

console.log(renameProperty(data.info, "id", "_id"));

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