Merge Array Items into String

Merge array items into string

Use the implode function.

For example:

$fruits = array('apples', 'pears', 'bananas');
echo implode(',', $fruits);

Merge array elements with \n into String in Swift

You should use joined(separator:). It joins the array elements and adds the given separator string between the elements.

let str = arrSectionName.joined(separator: " \n")

How to merge an array of objects into a single string?

Since you want a single string out of this, and not an array, you will want to use Array.forEach to concatenate onto an existing string object, like so:

let outStr = '';

array.forEach((ele, idx) =>
outStr += `${ele.key}: ${ele.text}${idx < array.length ? '' : ', '}`
);

You could also use Array.map like the folks in the comments above suggested, but you will need to join the result to produce a single string at the end.

Merge values from Array into a String

You could map the wwamted property name and join the array to a string.

Methods:

  • Array#map,

  • destructuring assignment for the property name,

  • Array#join with a custom glue.

Code:

result = this.$root.data[1].arrayOfObjects
.map(({ name }) => name)
.join(', ');

How to merge specific elements inside an array together?

You could join your array of elements into a string, and then use .split() to split your string by a sequence of numbers.

So first you can use .join('') to obtain:

"5534*52"

And then .split(/(\d+)/) to split this string on number sequences. Here \d+ means split by (one or more) numbers, where () is used to keep the sequence of numbers in the resulting splitted array. After the split you will get an array looking like:

["", "5534", "*", "52", ""]

You can then use .filter(Boolean) to keep only the truthy values in your array. In the above array, the truthy values are the non-empty strings. After the filter you will get an array looking like:

["5534", "*", "52"]

Lastly, you can convert the string numbers into numbers, by mapping over the above array using .map(). If the element is a number, you can convert it to a number using the unary plus operator +x, otherwise, you can leave it as a string. After the map, you get your final result:

[5534, "*", 52]

See working example below:

const arr = [5,5,3,4,'*',5,2];

const merged = arr.join('').split(/(\d+)/).filter(Boolean).map(x => isNaN(x) ? x : +x);
console.log(merged);

How to merge array of strings with an array of ints

Such a case is a good candidate for using Map as shown below:

import java.util.LinkedHashMap;
import java.util.Map;

class Main {
public static void main(String[] args) {
String[] strArr = { "Cat", "Dog", "Egg" };
int[] intArr = { 3, 4, 5 };
Map<String, Integer> map = new LinkedHashMap<>();
for (int i = 0, n = Math.min(strArr.length, intArr.length); i < n; i++) {
map.put(strArr[i], intArr[i]);
}

System.out.println(map);
}
}

Output:

{Cat=3, Dog=4, Egg=5}

Alternatively, you can combine each record (the string and its corresponding number) into a string and put it into a String[].

Demo:

import java.util.Arrays;

class Main {
public static void main(String[] args) {
String[] strArr = { "Cat", "Dog", "Egg" };
int[] intArr = { 3, 4, 5 };
String[] result = new String[strArr.length];
for (int i = 0, n = Math.min(strArr.length, intArr.length); i < n; i++) {
result[i] = strArr[i] + " = " + intArr[i];
}

// Display
System.out.println(Arrays.toString(result));
}
}

Output:

[Cat = 3, Dog = 4, Egg = 5]

How to combine column values from an array of objects to a single object with string values in JavaScript

This looks like a good case for reduce, it will loop over every element in your array letting you keep track of attributes across all elements.

prev starts out as whatever you gave in the second parameter (in our case it's {})

next is an item from your array, starting at the 0th

Use Object.keys to loop over every attribute in your array element, adding them to the originally blank object and passing it along.

prev starts empty but after the first loop it holds all properties of element 1, second loop it has element 1 + element 2, etc until it finishes looping.

let myArray = [ {name: 'user 100', name2: 'user200', name3: 'user300'},{name: 'user 101', name2: 'user201', name3: 'user301'}, {name: 'user 102', name2: 'user202', name3: 'user302'} ];
let combined = myArray.reduce((prev, next) => { Object.keys(next).forEach(key => { prev[key] = (prev[key] ? prev[key] + ' ' : '') + next[key]; }); return prev;}, {}) console.log(combined);

Merge/flatten an array of arrays



ES2019

ES2019 introduced the Array.prototype.flat() method which you could use to flatten the arrays. It is compatible with most environments, although it is only available in Node.js starting with version 11, and not at all in Internet Explorer.

const arrays = [
["$6"],
["$12"],
["$25"],
["$25"],
["$18"],
["$22"],
["$10"]
];
const merge3 = arrays.flat(1); //The depth level specifying how deep a nested array structure should be flattened. Defaults to 1.
console.log(merge3);


Related Topics



Leave a reply



Submit