settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Compose an object by given properties in JavaScript

+2 votes

Write JavaScript function (or JS program) that composes an object by given properties. There will be 3 sets of property-value pairs (a total of 6 elements). Assign each value to its respective property of an object and return the object as a result of the function.

The input comes as an array of string elements.

Examples:

Input:
['name', 'Pesho', 'age', '23', 'gender', 'male']

Output:
{ name: 'Pesho', age: '23', gender: 'male' }


Input:
['ssid', '90127461', 'status', 'admin', 'expires', '600']

Output:
{ ssid: '90127461', status: 'admin', expires: '600' }

The output should be returned as a value.

asked in JavaScript category by user paulcabalit

1 Answer

+1 vote

Here is the solution to this js problem:

function assignProperty(arr) {
    let [prop1,value1,prop2,value2,prop3,value3]=arr;

    let obj = {};//key:value
    obj[prop1] = value1;
    obj[prop2] = value2;
    obj[prop3] = value3;

    //return obj;
    console.log(obj);
}

assignProperty(['ssid', '90127461', 'status', 'admin', 'expires', '600']);

 

answered by user hues
...