Last active
November 10, 2023 07:51
-
-
Save mir4ef/2c41692c6112495e9caceaff70a5a54c to your computer and use it in GitHub Desktop.
insert an element into a sorted array of objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1d array | |
for (var i = 0, len = arr.length; i < len; i++) { | |
if (somevalue < arr[i]) { | |
arr.splice(i, 0, somevalue); | |
break; | |
} | |
} | |
return arr; | |
// an array of objects | |
var newObj = { | |
key: value | |
}; | |
for (var i = 0, len = arr.length; i < len; i++) { | |
if (somevalue < arr[i].key) { | |
arr.splice(i, 0, newObj); | |
break; | |
} | |
} | |
return arr; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Errors in the splice statement in this code also I will have post my code when I repair my laptop