Mapping Object To Convert Object To Array
Solution 1:
Arrow functions are like functions, but with a different syntax. Normal functions use the syntax
function(x, y){
return x + y;
}
Arrow functions use the one-line syntax
(x, y) => x + y
or the multi-line syntax
(x, y) => {
return x + y;
}
In your example,
returnObject.keys(obj).map(k => [k, obj[k]]);
translates to
returnObject.keys(obj).map(function(k) {
return [k, obj[k]];
});
since the function takes one parameter k
(before the arrow) and returns [k, obj[i]]
(after the arrow).
Solution 2:
This is your second function:
functionconvertObjectToList(obj) {
Object.keys(obj).map(function(key){
obj = [key, obj[key]];
});
console.log(obj);
}
There are a few things that is out of place here:
- Your second function does not have a return value. So function call convertObjectToList will return undefined.
- You are reassigning the value of
obj
with an array[key, obj[key]]
, which is not what you want to do. That is why you are getting ['role' , undefined], because the last key ofobj
is role, and since obj has been modified to an array an not an object,obj['role']
will return undefined. - You need a return value inside the map, otherwise each element in the returned array will be undefined.
So the correct code would be the following:
var obj = {
name: 'Holly',
age: 35,
role: 'producer'
};
functionconvertObjectToList(obj) {
returnObject.keys(obj).map(function(key){
let currElement = [key, obj[key]];
return currElement
});
}
var res = convertObjectToList(obj);
console.log(res);
Solution 3:
It should be so
var obj = {
name: 'Holly',
age: 35,
role: 'producer'
};
functionconvertObjectToList(obj) {
returnObject.keys(obj).map(function (k) {
return [k, obj[k]];
})
}
convertObjectToList(obj);
Solution 4:
When using arrow function you could avoid the return statement if the arrow function contains only one line. So when using regular function you need to return manually
var obj = {
name: 'Holly',
age: 35,
role: 'producer'
};
functionconvertObjectToList(obj) {
returnObject.keys(obj).map(function(k) {
return [k, obj[k]]
});
}
var result = convertObjectToList(obj);
console.log(result);
Solution 5:
Object.keys returns an array which elements are strings corresponding to the enumerable properties found directly in obj. The order of properties is the same as that given by manually cycling on the properties of the object.
var arr = ["a", "b", "c"];
alert(Object.keys(arr)); // chiama alert con argomento "0,1,2"// array like objectvar obj = { 0 : "a", 1 : "b", 2 : "c"};
alert(Object.keys(obj)); // chiama alert con argomento "0,1,2"
Assuming you are passing your object , the map is built likethis, that's your result.
functionobjToStrMap(obj) {
const strMap = newMap();
for (const k ofObject.keys(obj)) {
strMap.set(k, obj[k]);
}
return strMap;
}
In your code try this change
functionconvertObjectToList(obj) {
Object.keys(obj).map(function(key){
return [key, obj[key]]; //obj = [key, obj[key]];
});
}
Post a Comment for "Mapping Object To Convert Object To Array"