Q: Convert object to an array and assign its position accordingly I need help to solve the problem below. I have this object that has 4 quarters from Q1 to Q4; however, there is one missing which is Q3. The goal is to convert the object to an array and assign the values to where its index should be. In this case, Q1 should be in 0 index, Q2 is in 1 index, and so on. Q3 is missing so it should be assigned to 0. Could someone help me with this? Thanks.
let obj = {Q1: 13, Q2: 5, Q4: 3};

// here is the output 
[13, 5, 0, 3]

A: You could use a destructuring for the properties with default values and get an array.


const
    convert = ({ Q1 = 0, Q2 = 0, Q3 = 0, Q4 = 0 }) => [Q1, Q2, Q3, Q4];

let obj = { Q1: 13, Q2: 5, Q4: 3 },
    result = convert(obj);

console.log(result);



A: I do this:


let obj = {Q1: 13, Q2: 5, Q4: 3};

console.log (Object.values(Object.assign({Q1:0,Q2:0,Q3:0,Q4:0},obj)))


