I have an array of objects
Using a for loop i am re-ordering my array of objects based on the property phoneType's value. Since this is a data response from an api, i am saying always place the phone number that has a phoneType of Primary first in my return arrary.
Question for you...I am not sure if this is the best way to do this. Is there a different way to accomplish this? I was thinking about using Object.Keys to do this, but could not figure it out.
Here is my loop:
export function examplefunctionForStackOverflow(phoneNumbers) {
const phoneNumberArray = [];
if (typeof phoneNumbers !== 'undefined') {
for (const i in phoneNumbers) {
const number = phoneNumbers[i].number;
const phoneType = phoneNumbers[i].phoneType;
phoneNumbers[i].phoneType === "Primary" ?
phoneNumberArray.unshift({
key: phoneType,
value: number
}) :
phoneNumberArray.push({
key: phoneType,
value: number
});
}
}
return phoneNumberArray;
}
data: {
phoneNumbers: [{
number: "(999) 999-9999",
extension: "",
phoneType: "CELL"
},
{
number: "(888) 888-8888",
extension: "",
phoneType: "Primary"
},
{
number: "(777) 777-7777",
extension: "777",
phoneType: "WORK"
}
]