An array of about 5 or more javascript objects in this format [{'a':'man', 'age':'35'},{'b':'woman', 'age':'30'}]
needs to be converted to [{gender:'man','age':'35'},{gender:'woman','age':'30'}]
.
What is an efficient way to do it?
edit
Also the input array may be like this where the keys are switched around:
[{'a':'man', 'age':'35'},{'age':'30', 'b':'woman'}]
where a, b, ... could be anything and the objects in the array could up to 10 objects.
An array of about 5 or more javascript objects in this format [{'a':'man', 'age':'35'},{'b':'woman', 'age':'30'}]
needs to be converted to [{gender:'man','age':'35'},{gender:'woman','age':'30'}]
.
What is an efficient way to do it?
edit
Also the input array may be like this where the keys are switched around:
[{'a':'man', 'age':'35'},{'age':'30', 'b':'woman'}]
where a, b, ... could be anything and the objects in the array could up to 10 objects.
-
Is it always
a
andb
or do you have to check the value ? – adeneo Commented Feb 5, 2017 at 21:07 - Efficient pared to what? What are you doing right now that you perceive to be insufficient? – user1106925 Commented Feb 5, 2017 at 21:10
- What's wrong with @NinaScholz answer then? – vp_arth Commented Feb 5, 2017 at 21:21
-
So
a
andb
could be anything, but are there other properties thanage
in those objects, and if so, how do you propose we'd filter them out ? – adeneo Commented Feb 5, 2017 at 21:22 - @adeneo No, there is no other property than age. – Fred J. Commented Feb 5, 2017 at 21:23
4 Answers
Reset to default 7You can iterate through original array and map fields you have to fields you need:
var array=[{'a':'man', 'age':'35'},{'b':'woman', 'age':'30'}];
var newArray = array.map(function(item){
return {
age: item.age,
gender: item.a || item.b
};
});
console.log(newArray);
You could iterate the array and iterate the keys and change only the unknown key to gender
.
var data = [{ a: 'man', age: '35' }, { b: 'woman', age: '30' }];
data.forEach(function (o) {
Object.keys(o).forEach(function (k) {
if (k !== 'age') {
o.gender = o[k];
delete o[k];
}
});
});
console.log(data);
You should use map
method which accepts as parameter a callback
function.
Also, you have to use Object.keys(item)[0]
in order to get the first key.
var array=[{'a':'man', 'age':'35'},{'b':'woman', 'age':'30'}];
console.log(array.map(function(item){
return {gender: item[Object.keys(item)[0]],age:item.age};
}));
Like others have e up with suggestions, this is how I would do it:
I would map the keys, to the correct keys a|b => gender, age => age
Or define a function, which should give me the right key, based on the value (or type)..
Simple Mapping
const keysMap = {
gender: ['a', 'b'],
age: 'age'
};
// get it once.
const rightKeys = Object.keys(keysMap);
const rightKeysLen = rightKeys.length;
function getRightKeyBy(key) {
for (let i = 0; i < rightKeysLen; i++ ) {
const condition = keysMap[rightKeys[i]];
if (key === condition ||
(condition instanceof Array && condition.indexOf(key) > -1)) {
return rightKeys[i]
}
}
// if no better key was found return the initial key
return key;
}
function convertKeys(obj) {
const newObj = {};
Object.keys(obj).forEach(key => {
newObj[getRightKeyBy(key)] = obj[key];
});
return newObj;
}
var array = [{a: 'men', age: '25'},{b: 'woman', age: '35'}];
var correctArray = array.map(convertKeys);
console.log(correctArray);