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

253
Vistas
Iterate through array of objects and remove unwanted symbols form array

I need to change this array of objects so that the strings are turned into numbers and the arrays are turned into something I can interate through. Im trying to make a table out of this. Heres what I tried so far:

function ParseData(obj) {
    const keys = Object.keys(obj);
    for (let i = 0; i < keys.length; i++) {
        obj[keys[i]] = JSON.parse(obj[keys[i]]);
    }
}
console.log(ParseData(newData));




const newData = [
    {
        apac: "[\"CN\",\"JP\"]"
        brand: '1',
        brands: '1183',
    },
    {
    apac: "[\"CN\",\"JP\"]"
        brand: '1',
        brands: '1183',
    },
];
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Just loop through the array, and use ParseData function to update keys of each object in the array.

You dont have to return a value from the function, if you are updating the paramater itself. All the changes made to the parameter will be available in the newData variable.

const newData = [
  { apac: "[\"CN\",\"JP\"]", brand: '1', brands: '1183' },
  { apac: "[\"CN\",\"JP\"]", brand: '1', brands: '1183' },
];
function ParseData(myArray) {
  myArray.forEach((node) => {
    const keys = Object.keys(node);
    for (let i = 0; i < keys.length; i++) {
      node[keys[i]] = JSON.parse(node[keys[i]]);
    }
  })
}
ParseData(newData);
console.log(newData)

If you want to make your newData unchanged and ParseData function return a new variable, you could store your paramater in a variable an a shallow copy. I used JSON.parse(JSON.stringify(myArray)) for that.

Working Fiddle

const newData = [
  { apac: "[\"CN\",\"JP\"]", brand: '1', brands: '1183' },
  { apac: "[\"CN\",\"JP\"]", brand: '1', brands: '1183' },
];
function ParseData(myArray) {
  const clonedata = JSON.parse(JSON.stringify(myArray));
  clonedata.forEach((node) => {
    const keys = Object.keys(node);
    for (let i = 0; i < keys.length; i++) {
      node[keys[i]] = JSON.parse(node[keys[i]]);
    }
  })
  return clonedata;
}
const output = ParseData(newData);
console.log(newData);
console.log(output);

about 4 years ago · Juan Pablo Isaza Denunciar

0

newData is not an object, it is an array of objects, so you don't need to use Object.keys(). Also your function doesn't return a value so your console log won't output anything.

Since keys within your objects are always the same you can do this:

const newData = [
    {
        apac: "[\"CN\",\"JP\"]",
        brand: '1',
        brands: '1183',
    },
    {
            apac: "[\"CN\",\"JP\"]",
        brand: '1',
        brands: '1183',
    },
];

function ParseData(arr) {
    for (let i = 0; i < arr.length; i++) {
        obj[i]['apac'] = JSON.parse(obj[i]['apac']);
        obj[i]['brand'] = parseInt(obj[i]['brand']);
        obj[i]['brands'] = parseInt(obj[i]['brands']);
    }
    return arr
}

console.log(ParseData(newData));

https://jsfiddle.net/t57u3hyv/1/

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try this one you get your output

<script>
const newData = [
    {
        apac: "[\"CN\",\"JP\"]",
        brand: '1',
        brands: '1183',
    },
    {
    apac: "[\"CN\",\"JP\"]",
        brand: '1',
        brands: '1183',
    },
];

function ParseData(obj) {
    const keys = Object.keys(obj);
    
    for (let i = 0; i < keys.length; i++) {
        
        var b=JSON.stringify(obj[keys[i]]);
        str = b.replace(/\\/g, '');
        obj[keys[i]] = str;
    }
    return obj;
}
console.log(ParseData(newData));

</script>

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