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' }
]
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:
data
to touple (for getting it converted using Object.fromEntries
)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
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP