0

I spent all night working on this, and I know the answer is close; but I still have a lot of syntax work to do. Please do not downvote this - doing so only discourages newbies like me from asking questions. I need to create a function that accepts simple objects as an argument, and returns an array of arrays. Function should work on objects with any number of columns of properties with a simple string/number value. I'll show and explain my work in the codes I'll attach below:

//my first code:
var hi = {a: 1, b: 2, c: 3};
var props = new Array([],[],[]);
var lent = 0;
function newWave(hi){
  for(var key in hi){
    props[lent].push(key);
    props[lent].push(hi[key]);
    lent = lent + 1;
  }
  return props;
}
newWave(hi);

//function yields: [ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ] ]
//The return output is correct, but I need a universal code; 
//looking at the number of columns in variable 'props', you can 
//tell that this function only works on objects with 3 roperties. 

//My second code:
function newWave(hi) {
  var props = [];
  var outcome = [];
  for (var i = 0; i<1; i++){
    for(var key in hi){
      props.push(key, hi[key]);
      outcome.push(props);
    }
    return outcome;
  }
}
newWave(hi);
//This returns:
//[ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ],
//  [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ],
//  [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ] ]
//I feel like the answer is so close, but it's been 7 hours working on this,
//your help is greatly appreciated

3 Answers 3

1
function newWave (hi) {
    return Object.keys(hi).map(v => [v, hi[v]]);
}

So here we use Object.keys to get an array of the object properties, and then for each of those we map them to an array thay has the property as first item and its value as second

Sign up to request clarification or add additional context in comments.

Comments

1

You don't have to define array before, you can just create new empty array in your function, loop passed object and push [key, value]

var hi = {a: 1, b: 2, c: 3};

function newWave(obj) {
  var r = []
  for (var key in obj) r.push([key, obj[key]])
  return r;
}

console.log(JSON.stringify(newWave(hi)))

Also there is new js method called Object.entries() that can return this result.

var hi = {a: 1, b: 2, c: 3};

function newWave(obj) {
  return Object.entries(obj)
}

console.log(newWave(hi))

2 Comments

Didn´t know about entries method, nice
Excellent shortcut, but for this assignment, 'return' is required, not console.log. And it had to be in the conventional outline of a function.
0

Use Object.keys to get all the keys present in the Object in an array. Loop over that array and push the keys and value to another array

var hi = {
  a: 1,
  b: 2,
  c: 3,
  d: 4
};
var finalArray = []

function newWave(hi) {
  var getKeys = Object.keys(hi);
  for (var i = 0; i < getKeys.length; i++) {
    var tempArray = [];
    tempArray.push(getKeys[i].toString(), hi[getKeys[i]])
    finalArray.push(tempArray)
  }
  return finalArray;
}

console.log(newWave(hi));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.