Created
October 18, 2022 08:04
-
-
Save masiur/97264bdc24309bb27779d40fbeed0a86 to your computer and use it in GitHub Desktop.
Javascript move left/right of elements in array
This file contains 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
let stage='completed'; | |
this.statuses = ['open', 'in-progress', 'completed']; | |
let index = this.statuses.indexOf(stage); | |
if (direction === 'left') { | |
if (index > 0) { | |
this.statuses.splice(index, 1); | |
this.statuses.splice(index - 1, 0, stage); | |
} | |
} else if (direction === 'right') { | |
if (index < this.statuses.length - 1) { | |
this.statuses.splice(index, 1); | |
this.statuses.splice(index + 1, 0, stage); | |
} | |
} | |
console.log(this.statuses); |
<?php
$stage = 'done';
$stages = [
(object) [
'g' => 'open',
'l' => 'Open'
],
(object) [
'g' => 'in_progress',
'l' => 'In Progress'
],
(object) [
'g' => 'done',
'l' => 'Done'
],
(object) [
'g' => 'ok',
'l' => 'ok'
]
];
$keyu = 10000;
foreach ($stages as $key => $item) {
if ($item->g == $stage) {
$keyu = $key;
break;
}
}
$key = $keyu;
$direction = 'left';
// echo $key;
// output 2
// begin using only array splice method
// $arraySpliced = array_splice($stages, $key, 1);
// $arraySpliced = array_splice($stages, $key-1, 0, $arraySpliced);
// end using only array splice method
// begin using slice way
$arraySliced = array_slice($stages, $key-1, 1);
// print_r($arraySliced);
// Array
// (
// [0] => stdClass Object
// (
// [g] => in_progress
// [l] => In Progress
// )
// )
$arraySliced2 = array_slice($stages, $key, 1);// (array, index, number of element to slice) take one element from array starting index $key
// print_r($arraySliced2);
// Array
// (
// [0] => stdClass Object
// (
// [g] => done
// [l] => Done
// )
// )
$arraySpliced = array_splice($stages, $key); // removed all element from index and return the removed part
// print_r($stages);
// Array
// (
// [0] => stdClass Object
// (
// [g] => open
// [l] => Open
// )
// [1] => stdClass Object
// (
// [g] => in_progress
// [l] => In Progress
// )
// )
// print_r($arraySpliced);
// Array
// (
// [0] => stdClass Object
// (
// [g] => done
// [l] => Done
// )
// [1] => stdClass Object
// (
// [g] => ok
// [l] => ok
// )
// )
array_shift($arraySpliced); // remove first element from 2nd part of the array
// print_r($arraySpliced);
// Array
// (
// [0] => stdClass Object
// (
// [g] => ok
// [l] => ok
// )
// )
$deleted = array_pop($stages); // remove last element from 1st part of the array
print_r($stages);
// Array
// (
// [0] => stdClass Object
// (
// [g] => open
// [l] => Open
// )
// )
// now merge first part, sliced parts and 2nd part or rest of the array
$merged = array_merge($stages, $arraySliced2, $arraySliced, $arraySpliced);
print_r($merged); // expected answer
// Array
// (
// [0] => stdClass Object
// (
// [g] => open
// [l] => Open
// )
// [1] => stdClass Object
// (
// [g] => done
// [l] => Done
// )
// [2] => stdClass Object
// (
// [g] => in_progress
// [l] => In Progress
// )
// [3] => stdClass Object
// (
// [g] => ok
// [l] => ok
// )
// )
// end using slice way
// begin using array index swqp
//if($direction == 'left') {
// if($key > 0) {
// $temp = $stages[$key - 1];
// $stages[$key - 1] = $stages[$key];
// $stages[$key] = $temp;
// }
// } elseif($direction == 'right') {
// if($key < count($stages) - 1) {
// $temp = $stages[$key + 1];
// $stages[$key + 1] = $stages[$key];
// $stages[$key] = $temp;
// }
// }
// end using array index swqp
?>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Same pupose php version