@@ -465,6 +465,54 @@ function array_rotate(arr, x) {
465
465
466
466
}
467
467
468
+ /**
469
+ * To find and upadate a key value in an array of objects
470
+ *
471
+ * @param array arr
472
+ * @param string key (key to be updated)
473
+ * @param string value (old value of the key )
474
+ * @param string newValue (new value of the key)
475
+ * @returns arr
476
+ */
477
+
478
+ function find_and_update ( arr , key , value , new_value ) {
479
+ arr . forEach ( element => {
480
+ if ( element [ key ] == value ) {
481
+ element [ key ] = new_value
482
+ }
483
+ } )
484
+ return arr
485
+ }
486
+
487
+
488
+ /**
489
+ * To find and update a key value in an array of
490
+ * objects with respect to a parent key value.
491
+ *
492
+ * @param array arr
493
+ * @param string parentKey (parent key to select the object)
494
+ * @param string parentValue (parent value to select the object)
495
+ * @param string targetKey (key to be updated)
496
+ * @param string targetValue (old value of the key to select only desired object)
497
+ * @param string targetValue | "*" to update all the targetKey
498
+ * @param string newValue (new value of the key)
499
+ * @returns arr
500
+ */
501
+
502
+
503
+ function find_key_and_update ( arr , parent_key , parent_value , target_key , target_value , new_value ) {
504
+ arr . forEach ( element => {
505
+ if ( element [ parent_key ] == parent_value ) {
506
+ if ( target_value == "*" ) {
507
+ element [ target_key ] = new_value
508
+ }
509
+ else if ( element [ target_key ] == target_value ) {
510
+ element [ target_key ] = new_value
511
+ }
512
+ }
513
+ } )
514
+ return arr
515
+ }
468
516
469
517
export {
470
518
is_array ,
@@ -491,4 +539,6 @@ export {
491
539
array_rotate ,
492
540
equilibrium_Point ,
493
541
get_rms_value ,
542
+ find_key_and_update ,
543
+ find_and_update ,
494
544
} ;
0 commit comments