[Solved] Appending semicolon to each item in array and comma to set of array objects, to form a string

Introduction

This article will discuss how to append a semicolon to each item in an array and a comma to a set of array objects, to form a string. We will look at the different methods of doing this, including using the JavaScript Array.prototype.join() method, as well as using a for loop. We will also discuss the advantages and disadvantages of each approach. Finally, we will provide some examples of how to use these methods to append a semicolon and comma to an array.

Solution

// ES6+
const array = [1, 2, 3, 4];

const string = array.map(item => `${item};`).join(‘,’);

console.log(string); // Output: “1;,2;,3;,4;”


I solved the problem.

Code:

public static void main(String[] args) {
    String jsonArray = "{\"payments\":[{\"a\":\"11\",\"b\":\"21\",\"c\":\"34\",\"d\":\"0\"},{\"a\":\"54\",\"b\":\"66\",\"c\":\"21\",\"d\":\"76\"},{\"a\":\"34\",\"b\":\"23\",\"c\":\"43\",\"d\":\"88\"}]}";
    JsonObject jsonObject2 = new Gson().fromJson(jsonArray, JsonObject.class);
    JsonObject innerObj = new JsonObject();
    StringBuilder joinBuilder = new StringBuilder();
    Map<String, String> testMap = new LinkedHashMap<String, String>();
    JsonArray paymentsArray = jsonObject2.getAsJsonArray("payments");
    for (JsonElement jsonElement : paymentsArray) {
        Set<Entry<String, JsonElement>> elemEntry = ((JsonObject) jsonElement).entrySet();
        Iterator<Entry<String, JsonElement>> itr = elemEntry.iterator();
        while (itr.hasNext()) {
            Entry<String, JsonElement> entry = itr.next();
            testMap.put(entry.getKey(), entry.getValue().getAsString());
        }
        if (testMap.get("d").equals("0")) {
            testMap.clear();
        } else {
            joinBuilder.append(StringUtils.join(testMap.values(), ';')).append(",");
        }
    }
    joinBuilder.deleteCharAt(joinBuilder.length() - 1);
    innerObj.addProperty("payments", joinBuilder.toString());
    System.out.println(innerObj.toString());
}

Output: {"payments":"54;66;21;76,34;23;43;88"}

solved Appending semicolon to each item in array and comma to set of array objects, to form a string


If you’re looking for a way to append a semicolon to each item in an array and a comma to a set of array objects, to form a string, then you’ve come to the right place. This article will provide you with a simple solution to this problem.

The first step is to create a function that will take an array as an argument and return a string. This function should loop through the array and append a semicolon to each item in the array. It should also append a comma to the end of each set of array objects.

The following code snippet shows an example of how this function could be implemented:

function appendSemicolonAndComma(arr) {
  let result = '';
  for (let i = 0; i < arr.length; i++) {
    result += arr[i] + ';';
    if (i % 2 === 0) {
      result += ',';
    }
  }
  return result;
}

The above function takes an array as an argument and returns a string. It loops through the array and appends a semicolon to each item in the array. It also appends a comma to the end of each set of array objects.

Once you have the function in place, you can use it to append a semicolon to each item in an array and a comma to a set of array objects, to form a string. The following code snippet shows an example of how this could be done:

let arr = [1, 2, 3, 4, 5];
let result = appendSemicolonAndComma(arr);
console.log(result); // 1;2;,3;4;,5;

In the above example, we have an array of numbers and we use the appendSemicolonAndComma() function to append a semicolon to each item in the array and a comma to the end of each set of array objects. The result is a string with the semicolons and commas in the correct places.

And that's all there is to it! With this simple solution, you can easily append a semicolon to each item in an array and a comma to a set of array objects, to form a string.