You could use recursion to find the insertion point, passing the index around:
function insertZ(object, children, index = 0) {
if (index >= children.length || children[index].z < object.z) {
return [
...children.slice(0, index),
object,
...children.slice(index),
];
}
return insertZ(object, children, index + 1);
}
Live Example:
let children = [{ z: 10 }, { z: 10 }, { z: 0 }, { z: 0 }, { z: -1 }, { z: -1 }];
function insertZ(object, children, index = 0) {
if (index >= children.length || children[index].z < object.z) {
return [
...children.slice(0, index),
object,
...children.slice(index),
];
}
return insertZ(object, children, index + 1);
}
let object1 = { z: 7, name: "new guy" }; // after the 10s, before the 0s
console.log(insertZ(object1, children));
let object2 = { z: 10, name: "new guy" }; // after the 10s, before the 0s
console.log(insertZ(object2, children));
let object3 = { z: -1, name: "new guy" }; // after the -1s
console.log(insertZ(object3, children));
let object4 = { z: 90, name: "new guy" }; // before everything
console.log(insertZ(object4, children));
// Testing adding to an empty array
console.log(insertZ(object4, []));
.as-console-wrapper {
max-height: 100% !important;
}
If desired, to avoid callers passing duff indexes, the work could be done by a private inner function:
let children = [{ z: 10 }, { z: 10 }, { z: 0 }, { z: 0 }, { z: -1 }, { z: -1 }];
function insertZ(object, children) {
const worker = (object, dhildren, index = 0) => {
if (index >= children.length || children[index].z < object.z) {
return [
...children.slice(0, index),
object,
...children.slice(index),
];
}
return worker(object, children, index + 1);
};
return worker(object, children);
}
let object1 = { z: 7, name: "new guy" }; // after the 10s, before the 0s
console.log(insertZ(object1, children));
let object2 = { z: 10, name: "new guy" }; // after the 10s, before the 0s
console.log(insertZ(object2, children));
let object3 = { z: -1, name: "new guy" }; // after the -1s
console.log(insertZ(object3, children));
let object4 = { z: 90, name: "new guy" }; // before everything
console.log(insertZ(object4, children));
// Testing adding to an empty array
console.log(insertZ(object4, []));
.as-console-wrapper {
max-height: 100% !important;
}
Alternatively, you could use recursion with a bunch of temporary arrays:
function insertZ(object, children) {
if (children.length === 0) {
return [object];
}
const [first] = children;
if (first.z < object.z) {
return [object, ...children];
}
return [first, ...insertZ(object, children.slice(1))];
}
Live Example:
let children = [{ z: 10 }, { z: 10 }, { z: 0 }, { z: 0 }, { z: -1 }, { z: -1 }];
function insertZ(object, children) {
if (children.length === 0) {
return [object];
}
const [first] = children;
if (first.z < object.z) {
return [object, ...children];
}
return [first, ...insertZ(object, children.slice(1))];
}
let object1 = { z: 7, name: "new guy" }; // after the 10s, before the 0s
console.log(insertZ(object1, children));
let object2 = { z: 10, name: "new guy" }; // after the 10s, before the 0s
console.log(insertZ(object2, children));
let object3 = { z: -1, name: "new guy" }; // after the -1s
console.log(insertZ(object3, children));
let object4 = { z: 90, name: "new guy" }; // before everything
console.log(insertZ(object4, children));
// Testing adding to an empty array:
console.log(insertZ(object4, []));
.as-console-wrapper {
max-height: 100% !important;
}