I am working on a javascript object. And I want to change the sequence of the object.
This is my current object(I'm trying to move the last two key, value pairs of the object),
const obj = {
one: {
name: "one"
},
two: {
name: "two"
},
five: {
name: "five"
},
three: {
name: "three"
},
four: {
name: "four"
}
};
This is what I'm trying to achieve,
const obj = {
one: {
name: "one"
},
two: {
name: "two"
},
three: {
name: "three"
},
four: {
name: "four"
}
five: {
name: "five"
},
};
This is so far what I've done,
const numbers = ["one", "two", "three"];
const sortObject = (object) => {
let objOne = {};
let objTwo = {};
let index = 0;
Object.entries(object).forEach(([key, value]) => {
if (numbers.includes(key)) {
objOne[key] = value;
} else {
objTwo[key] = value;
}
});
for (const obj in objOne) {
if (index === 2) {
Object.assign(objOne, objTwo);
}
index++;
}
Object.entries(objOne).forEach((element) => {
console.log(element);
});
};
sortObject(obj);
Would it be possible to move the last two key, value pairs of the object to the third position of the object?