1
[ { 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
2
  • 1
    Please add the details on what you have done so far here. Commented Jul 22, 2020 at 4:35
  • Your output is enclosed in parentheses ? Commented Jul 22, 2020 at 4:37

5 Answers 5

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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));

1 Comment

Thank u sonEtLumiere
0

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))

Comments

0
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));

Comments

0

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.