TransWikia.com

I am currently working on Javascript and stuck with using for loop in objects

Stack Overflow Asked by abhi on November 18, 2021

[ { name: 'rajesh',
    type: 'varchar',
    length: 50,
    value: '07/21/2020' },
  { name: 'ramesh',
    type: 'varchar',
    length: 50,
    value: '07/21/2020' },
  { name: 'abhi', type: 'int', value: '5' }
 ]
  1. I need to use for loop
  2. I need output like this(‘rajesh’:’07/21/2020,’ramesh’:’07/21/2020′,’abhi’: 5
  3. Need to use JSON.stringify

5 Answers

const data = [{
    name: 'rajesh',
    type: 'varchar',
    length: 50,
    value: '07/21/2020'
  },
  {
    name: 'ramesh',
    type: 'varchar',
    length: 50,
    value: '07/21/2020'
  },
  {
    name: 'abhi',
    type: 'int',
    value: '5'
  }
]

const convertedData = Object.fromEntries(data.map(({
  name,
  value
}) => [name, value]))

console.log(convertedData)
console.log(JSON.stringify(convertedData))

Answered by abhi on November 18, 2021

let arr = [ { name: 'rajesh',
    type: 'varchar',
    length: 50,
    value: '07/21/2020' },
  { name: 'ramesh',
    type: 'varchar',
    length: 50,
    value: '07/21/2020' },
  { name: 'abhi', type: 'int', value: '5' }
 ];


 let result = {};

 for(i= 0; i < arr.length; i++){
    result[arr[i].name] = arr[i].value;
 }

 console.log(result);
 console.log(JSON.stringify(result));

Answered by abhi on November 18, 2021

something like this maybe,

const data = [{
    name: 'rajesh',
    type: 'varchar',
    length: 50,
    value: '07/21/2020'
  },
  {
    name: 'ramesh',
    type: 'varchar',
    length: 50,
    value: '07/21/2020'
  },
  {
    name: 'abhi',
    type: 'int',
    value: '5'
  }
]

const convertedData = Object.fromEntries(data.map(({
  name,
  value
}) => [name, value]))

console.log(convertedData)
console.log(JSON.stringify(convertedData))

Steps Taken:

  • Convert data to touple (for getting it converted using Object.fromEntries)
  • Create Object from the touples
  • Convert to JSON

Answered by aeXuser264 on November 18, 2021

I believe this is what you're looking for:

const data = [
  {
    name: 'rajesh',
    type: 'varchar',
    length: 50,
    value: '07/21/2020'
  },
  {
    name: 'ramesh',
    type: 'varchar',
    length: 50,
    value: '07/21/2020'
  },
  {
    name: 'abhi',
    type: 'int',
    value: '5'
  }
]

const result = data.reduce((acc, curr) => {
  acc[curr.name] = curr.value
  return acc
}, {})

console.log(result, JSON.stringify(result))

Answered by Karma Blackshaw on November 18, 2021

Try this

let arr = [ { name: 'rajesh',
    type: 'varchar',
    length: 50,
    value: '07/21/2020' },
  { name: 'ramesh',
    type: 'varchar',
    length: 50,
    value: '07/21/2020' },
  { name: 'abhi', type: 'int', value: '5' }
 ];


 let result = {};

 for(i= 0; i < arr.length; i++){
    result[arr[i].name] = arr[i].value;
 }

 console.log(result);
 console.log(JSON.stringify(result));

Answered by sonEtLumiere on November 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP