Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

95
Views
¿Cómo creo una función que cambiará la propiedad dentro de un objeto que se encuentra dentro de una matriz, en un JSON?

así que he estado atascado en una tarea de JavaScript durante horas.

así que me han dado un archivo Json con los siguientes datos:

 { "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 } ] }

La instrucción para mi asignación es la siguiente:

Instrucción

Entonces, por lo que he entendido, necesito crear una función que contenga dos parámetros de cadena, y necesitaré cambiar la propiedad dentro de mi objeto que se encuentra en una matriz a una nueva propiedad.

Y hasta ahora esto es lo que hice:

 'use strict' const iceCream = require('./main.json') let namespace = { changeProp : function (newprop,oldprop) { for (let oldprop in iceCream.info) { } } }

Sé que no es mucho en absoluto, pero solo quiero un poco de ayuda para saber cómo puedo avanzar un poco más en mi tarea.

Cualquier ayuda se agradece, gracias

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puede reemplazar fácilmente oldKey con newKey usando map , Object.keys y 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 Report

0

Así es como puedes hacerlo. Solución fácil, simple y desagradable.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!