Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

66
Visualizações
how to get only one value from array data and convert it into comma separted values. using javascript without template literals

i have array data in the format given below

const data=  [
  {
    name: "productname",
    id: "1356",
    price: "0.00",
    category: "Health",
    position: "1",
    list: "New Products",
    stocklevel: "20",
    brand: "Health"
  },
  {
    name: "productname2",
    id: "5263",
    price: "0",
    category: "Hair",
    position: "2",
    list: "New Products",
    stocklevel: "",
    brand: "Hair"
  }]

from this data i want only product name of each product by difference of product1 , product2. for example i want the data in format of string by comma separated values like given below:-

product1name: "productname",
product2name: "productname2",
...

i tried using map function but not able to take only one or two values from whole array data. here is the code what i tried

    var dataByComa = '';

  var Values = data
  .map(function (p, i) {
    return Object.keys(data[i]).map(function (k) {
      return "prod" +" " + ": " + JSON.stringify(data[i][k]);
    });
  }).map(function (v) {
    return v.join(",\n"); });
  var commaValues = Values.join(",\n");
return commaValues;

with this code i can able to convert array data into comma separated values but i want only productnames.

Note :- Without using template template literals.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can do that using reduce. It takes a function as first and an initial value as second parameter (here an empty object {}). The variable "previous" keeps track of the current state of the (initially empty) new object while the function adds the name of every single Element from the "data" array to the object.

var result = data.reduce((previous, element) => {
    previous[element.name] = element.name;
    return previous;
}, {})

EDIT: As i realized, you actually want a string as result. In that case you could do:

var result = data.reduce((previous, element) => {
    previous[element.name] = element.name;
    return previous;
}, {})
var csvWithBrackets = JSON.stringify(result)
var csv = csvWithBrackets.substring(1, csvWithBrackets.length-1)

However the answer from Pzutils seems more compact.

about 4 years ago · Juan Pablo Isaza Relatório

0

Edit: I'm more clear on what the OP was looking for now - here is an answer that fulfills all the requirements

let commaValues = ""
for (let i = 0; i < data.length; i++) {
    commaValues += ("product" + (i + 1) + "name: " + "\"" + data[i]["name"] + "\", ")
}
// result: 'product1name: "productname", product2name: "productname2", '
about 4 years ago · Juan Pablo Isaza Relatório

0

You can iterate through the data and assign the value to an external variable like this:

    let finalData = {}
    data.forEach((item, index) => {
       finalData[`product${index+1}name`] = item.name
    })
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda