JavaScript function to prepend string into all the values of array?



In this article, we will learn to prepend a string into all the values of an array in JavaScript. Arrays are versatile data structures used to store multiple elements of similar or mixed types. A common operation is prepending a string to each element of an array, which involves adding a specified string at the beginning of each element.

Problem Statement

The problem statement can be deeply visualized as given a string text specifically to prepend or attach at the beginning of each element of the array , the output should return the new resultant array with prepended string value in combination with the value of the array elements.

Example

Input

const str = "Hello";
const stringArray = ["naman" , "priya" , "rishi"];

Output

[ 'Hellonaman', 'Hellopriya', 'Hellorishi' ]

What is an Array in JavaScript?

If you are familiar with any other programming language like C, C++, or Java, you must have heard the term 'array.' In programming, an array is a collection of similar data elements under one roof. An array is an object that stores multiple elements. Since an array is also an object, it has some properties and methods that make working with arrays easier in JavaScript.

Following is the syntax to define Arrays in JavaScript ?

const arrayExample  = [ 2 , 3 , 5 ,6 ];
console.log(arrayExample);

Output

[2, 3, 5, 6]

What is a prepend String in JavaScript?

Prepend a string in JavaScript means adding some specific string at the beginning of the input value given by the user that will result in a newly obtained string array combination.

Algorithm

  • Step 1 ? Declare a function with name prepend that takes string input to be prepended and string array as an input
  • Step 2 ? Traverse every element of the string array using a for loop till the length of string elements such that every string element present in the string array needs to be prepended with the input using the + operator.
  • Step 3 ? Once a for loop is used to prepend every string array element, the resultant array with its length property is returned because the prepending new string array replaces the original string array given as input by the user.

Example

Below is an example of prepending a string into all the values of the array in javascript ?

function prepend ( str  , stringArray )
{
    for(let i = 0 ; i<stringArray.length ;i++)
    {
        stringArray[i] = `${str}` + stringArray[i];
       
    }
    
    return stringArray.length;
}

const str = "Hello";
const stringArray = ["naman" , "priya" , "rishi"];
prepend(str,stringArray);
console.log(stringArray);

Output

[ 'Hellonaman', 'Hellopriya', 'Hellorishi' ]

Time complexity: O(n), where n is the length of string array, 

Space complexity: O(1), in-place modification of the array.

Conclusion

In summary, prepending a string to all elements of a JavaScript array involves straightforward logic, leveraging the for loop for in-place modifications. This approach ensures efficient use of time and space while maintaining the original array structure. Such operations not only reinforce the concepts of array manipulation but also showcase JavaScript's flexibility in handling string and array operations seamlessly.

Updated on: 2025-01-16T20:23:16+05:30

733 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements