Skip to content

Instantly share code, notes, and snippets.

@kanglicheng
Last active September 28, 2022 23:38
Show Gist options
  • Save kanglicheng/6303843d5d15cc8053a0a50f786e6f76 to your computer and use it in GitHub Desktop.
Save kanglicheng/6303843d5d15cc8053a0a50f786e6f76 to your computer and use it in GitHub Desktop.
//Q1
//Write a function 'transformFirstAndLast' that takes in an array, and returns an object with:
//1) the first element of the array as the object's key, and
//2) the last element of the array as that key's value.
function transformFirstAndLast(array) {
//your code here
var ans ={}
ans[array[0]]=array[array.length-1]
return ans
}
//Q2 Write a function called "getAllKeys" which returns an array of all the input object's keys.
function getAllKeys(obj) {
var ans = [];
for(var k in obj) {
ans.push(k);
}
return ans;
}
//Q3
function fromListToObject(array)
{
var ans= {}
for (var i=0; i<array.length; i++)
{
ans[array[i][0]=array[i][1]
}
return ans
}
//Q4
function listAllValues(obj)
{
var ans =[];
for(var k in obj) {
ans.push(obj[k]);
}
return ans
}
//Q5
function transformEmployeeData(array) {
// your code here
var result = [];
for(var i = 0; i < array.length; i++) {
let tempObj = {};
for(var j = 0; j < array[i].length; j++) {
tempObj[array[i][j][0]] = array[i][j][1];
}
result.push(tempObj);
}
return result;
}
//Q6
function convertObjectToList(obj) {
// your code here
var result = [];
for (var i in obj) {
var pair = [];
pair.push(i, obj[i]);
result.push(pair);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment