@@ -353,6 +353,55 @@ function get_only(arr, keys) {
353353 ) ;
354354}
355355
356+ /**
357+ * To find and upadate a key value in an array of objects
358+ *
359+ * @param array arr
360+ * @param string key (key to be updated)
361+ * @param string value (old value of the key )
362+ * @param string newValue (new value of the key)
363+ * @returns arr
364+ */
365+
366+ function find_and_update ( arr , key , value , new_value ) {
367+ arr . forEach ( element => {
368+ if ( element [ key ] == value ) {
369+ element [ key ] = new_value
370+ }
371+ } )
372+ return arr
373+ }
374+
375+
376+ /**
377+ * To find and update a key value in an array of
378+ * objects with respect to a parent key value.
379+ *
380+ * @param array arr
381+ * @param string parentKey (parent key to select the object)
382+ * @param string parentValue (parent value to select the object)
383+ * @param string targetKey (key to be updated)
384+ * @param string targetValue (old value of the key to select only desired object)
385+ * @param string targetValue | "*" to update all the targetKey
386+ * @param string newValue (new value of the key)
387+ * @returns arr
388+ */
389+
390+
391+ function find_key_and_update ( arr , parent_key , parent_value , target_key , target_value , new_value ) {
392+ arr . forEach ( element => {
393+ if ( element [ parent_key ] == parent_value ) {
394+ if ( target_value == "*" ) {
395+ element [ target_key ] = new_value
396+ }
397+ else if ( element [ target_key ] == target_value ) {
398+ element [ target_key ] = new_value
399+ }
400+ }
401+ } )
402+ return arr
403+ }
404+
356405export {
357406 is_array ,
358407 is_num_array ,
@@ -374,4 +423,6 @@ export {
374423 difference ,
375424 sanitize_array ,
376425 get_only ,
426+ find_key_and_update ,
427+ find_and_update ,
377428} ;
0 commit comments