Long story short, I got rocked on a code pairing interview question and still can’t find a great way to solve it. Any thoughts? I knew that manipulating objects were a weak point, guess it’s time to revisit objects.
Question is as follows:
Given an input = [
{foo: 10, bar: 20},
{foo: 12, bar: 30}
]
Write a function that results in this output = {foo: [10, 12], bar: [20,30]}
The keys foo and bar are unknown.
Hints are that Object.keys() will be needed and a double for loop is likely.
// put all of the properties in the the objArr array into
// the corresponding properties of testObj
function test(objArr) {
const testObj = {
foo: [],
bar: []
}
}
If you know how you would do that, then how could you create testObj if it wasn’t given to you?
With the following function I am able to get the required output. I am struggling with how to incorporate Object.keys now. If I loop through Object.keys(input[i]) and assign to an array, I get two arrays of keys: keys = [ [foo, bar], [foo, bar] ]. I could then reduce the keys array down to find unique values but I have a hunch that there is a better way to do this that would bypass the reduce.
function convert(input){
const foo = []
const bar = []
let obj ={}
for(let i = 0; i < input.length; i++){
foo.push(input[i]['foo'])
bar.push(input[i]['bar'])
}
obj['foo'] = foo
obj['bar'] = bar
return obj //returns {foo: [10, 12], bar: [20, 30]}
}
Can you take a look to the output please.
Is it possible you make a typing mistake?
You wrote:
output =
{foo: [10, 12],
bar: [12,30]
}
but correctly:
output =
{foo: [10, 12],
bar: [20,30]
}
???
If you made typing mistake here is my solution:
Summary
"use strict";
/*
Given an input = [
{foo: 10, bar: 20},
{foo: 12, bar: 30}
]
Write a function that results in this output = {foo: [10, 12], bar: [20, 30]}
The keys foo and bar are unknown.
Hints are that Object.keys() will be needed and a double for loop is likely.
*/
const myOutput = input => {
const output = {};
input.forEach(obj => {
for (let key in obj) {
if (!(output[key])) output[key] = [];
}
for (let key in obj) {
output[key].push(obj[key]);
}
});
return output;
}
const input = [
{foo: 10, bar: 20},
{foo: 12, bar: 30}
];
const unknownKeys = [
{unknown: 10, nobody: 20},
{unknown: 12, nobody: 30}
];
console.log(myOutput(input));
console.log(myOutput(unknownKeys));
I did not find any way to build the output in the same for loop but I also did not used Object.keys().
If your output is not a mistake, then I have to modify the function…
Ok,
than focus to the IMPORTANT information: The keys are unknown.
So do not use in your code
foo: anything
or
obj["foo"] = anything
or
foo.push(input[i]['foo'])
because the keys are unknown.
All of the above solutions are incorrect.
ps. edit: From now you can find the way to use Object.keys() too…
I’ll be curious will you find a way to build output with too loop only… Till now I have no idea…