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

147
Vistas
How to add double quotes to both keys and values for an array in javascript

I have a an array containing buffer data as below

 [{ Buffer_Data: <Buffer b5 eb 2d> },{ Buffer_Data: <Buffer b5 eb 2d> },{ Buffer_Data: <Buffer b5 eb 2d> },{ Buffer_Data: <Buffer b5 eb 2d> }]

I want double quotes to be added to both key and values and the result should be as below.

 [{ "Buffer_Data": "<Buffer b5 eb 2d>" },{ "Buffer_Data": "<Buffer b5 eb 2d>" },{ "Buffer_Data": "<Buffer b5 eb 2d>" },{ "Buffer_Data": "<Buffer b5 eb 2d>" }]

I have tried JSON.stringify but it is giving me the buffer data in invalid format. How can I convert this to array with double quotes. Please help

Here is my code

 stream = fs.createReadStream('files/uploaded_files/' + req.body.fileName);
          var bufferdata = [];
        stream.on('data', async function (chunk) {
            var obj = {};
            obj.Buffer_Data =  Buffer.from(chunk.toString('binary'), 'base64');
            bufferdata.push(obj)
        });
        stream.on('end', async function(){
        var new_buffer_data = JSON.stringify(bufferdata) // This is giving invalid value
      })

Code I tried

    stream = fs.createReadStream('files/uploaded_files/' + req.body.fileName);
          var bufferdata = [];
        stream.on('data', async function (chunk) {
            var bytes = chunk.map(str => parseInt(str, 16));
              const buf = Buffer.from(bytes);
              var obj = {};
              obj.Buffer_Data = buf;
            buffer.push(obj)
        });
        stream.on('end', async function(){
         const stringified = JSON.stringify(buffer.map(obj => ({Buffer_Data: stringifyBuffer(obj.Buffer_Data)})));
      
        console.log(stringified);
      })
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can stringify the array of objects with Buffer data in the format that you specified using this method:

./so-71389714.mjs:

import {Buffer} from 'buffer';

function stringifyBuffer (buf) {
  let result = '<Buffer';

  for (const byte of buf) {
    result += ` ${byte.toString(16)}`;
  }

  result += '>';
  return result;
}

const bytes = ['b5', 'eb', '2d'].map(str => parseInt(str, 16));
const buf = Buffer.from(bytes);
const obj = {Buffer_Data: buf};

// This is the array in your question
const array = [obj, obj, obj, obj];

const stringified = JSON.stringify(array.map(obj => ({Buffer_Data: stringifyBuffer(obj.Buffer_Data)})));

console.log(stringified);

$ node so-71389714.mjs
[{"Buffer_Data":"<Buffer b5 eb 2d>"},{"Buffer_Data":"<Buffer b5 eb 2d>"},{"Buffer_Data":"<Buffer b5 eb 2d>"},{"Buffer_Data":"<Buffer b5 eb 2d>"}]
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