0

Write a function called myFun which has an object as its parameter and returns the name of properties of that object in an array. For instance, if it receives {a:1,b:3}, as its parameter, it should return [a, b], or if it receives {u:4, k:3, h:5}, it should return [u, k, h].

Note I am aware of Object.Keys(object) returns ['a', 'b', 'c']

//this function should return the name of the property
function myFun(object) {
    object = {
        a: 1,
        b: 2,
        c: 3
    }
    for (obj in object) {
        console.log(obj);
    }
}
    
myFun();

//testcase : console.log(myFun({a:6})[0]) which should return [a], is it 
 actually possible or am I asking the wrong question?
  • `Object.keys(object)` – Krzysztof Krzeszewski Oct 19 '20 at 11:07
  • if this is the myObj = {u:4, k:3, h:5}; I was trying to output [u, k, h] while if I try Object.keys(myObj); I get something like ['u', 'k', 'h'] I want to avoid ' ' if that make sense? – MR. Stranger Oct 19 '20 at 11:16
  • @MR.Stranger that _doesn't_ make sense. The keys are strings, you cant just have `[u,k,h]` because that is not a thing - u k & h are not defined. What are you actually trying to do? – Jamiec Oct 19 '20 at 11:18

2 Answers2

0

To get an array of object keys:

const keys = Object.keys(object);

To print them like you describe:

console.log(`[${keys.join(',')}]`);

Putting it together as a function:

function myFun(object) {
    const keys = Object.keys(object);

    return `[${keys.join(',')}]`;
}
domenikk
  • 1,723
  • 1
  • 10
  • 12
  • Thanks @domenikk I assume this is one of the solution, could you please read the question and help me achieve this using a function, feel free to write a new piece of code and also ignore my code. – MR. Stranger Oct 19 '20 at 22:22
0

This is the object's keys

var obj = {
        a: 1,
        b: 2,
        c: 3
      };
      
console.log(Object.keys(obj));
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • I just want to print [a, b, c], is it possible? – MR. Stranger Oct 19 '20 at 11:21
  • @MR.Stranger no, because that doesn't make sense - as I said above a, b & c are just undefined variables. What are you actually trying to do? You have an [XY Problem](http://xyproblem.info) – Jamiec Oct 19 '20 at 11:22
  • Write a function called fun which has an object as its parameter and returns the name of properties of that object in an array. For instance, if it receives {a:1,b:3}, as its parameter, it should return [a, b], or if it receives {u:4, k:3, h:5}, it should return [u, k, h]. – MR. Stranger Oct 19 '20 at 11:24
  • @MR.Stranger "**name** of properties" should be a pretty good indication that you want strings. names are always strings. – Jamiec Oct 19 '20 at 11:37
  • @MR.Stranger check my edited answer – domenikk Oct 19 '20 at 11:44
  • haha yes I found this question as a warm up question for JavaScript and It's bit strange when the question says "Name of the properties"(which are always strings) but wanted to display in [u, k, h]. Thanks for the help @Jamiec – MR. Stranger Oct 19 '20 at 22:33