javascript
filter-Elite software solutions
inp = [ {name: 'firstname', value: 'johndoe'}, {name: 'age', value: '28'}, {name: 'language', value: 'English'}, {name: 'language', value: 'Tamil'}, {name: 'language', value: 'Hindi'}, ]; Expected Output - { firstname: 'johndoe', age: '28', language: ['English', 'Tamil', 'Hindi'] }
const inp = [
{name: 'firstname', value: 'johndoe'},
{name: 'age', value: '28'},
{name: 'language', value: 'English'},
{name: 'language', value: 'Tamil'},
{name: 'language', value: 'Hindi'},
];
const output = {};
for (const item of inp) {
if (item.name === 'firstname' || item.name === 'age') {
output[item.name] = item.value;
} else if (item.name === 'language') {
if (!output.language) {
output.language = [];
}
output.language.push(item.value);
}
}
console.log(output)
Output
{
firstname: 'johndoe',
age: '28',
language: ['English', 'Tamil', 'Hindi']
}
firstname: 'johndoe',
age: '28',
language: ['English', 'Tamil', 'Hindi']
}
Was this helpful?
Similar Posts