How can I convert an array to an object by splitting strings?

I have an array like below:

["gender-m", "age-20", "city-london", "lang-en", "support-home"]

I tried to generate a JSON object:

{"gender":"m", "age":"20", "city":"london", "lang":"en", "support":"home"}

One solution I can think of is using FOR loop to make it, but I am sure there are elegant solutions for this. Any suggestions, please help me.


You could take Object.fromEntries with the splitted key/value pairs.

var data = ["gender-m", "age-20", "city-london", "lang-en", "support-home"],
    result = Object.fromEntries(data.map(s => s.split('-')));

console.log(result);

What if you have a nested array? That is, an array with an array inside it? Here you need to use JSON.stringify(). Finally, if you can’t use a built-in method for some reason, you can loop through the array and convert it to a string manually.


I don't think there is an elegant way because you use the "-" sign to separate key and value. So, the best way would be:

let element = {};
array.forEach(item => {
result = item.split("-"); 
element[result[0]] = result[1];
}

Notice how each item in the new object uses the array index of the value as the key (i.e. "0", "1", etc.). And you can now access each item in the new object using the conventional dot notation method (foo.bar). In the next section, we'll show you how to manually loop over an array to construct a new object. 2. Loop Over Array & Construct a New


Use Array.reduce

const arr = ["gender-1", "age-m", "city-london", "lang-en", "support-home"]
arr.reduce((acc, c) => {
    const str = c.split('-')
    return {...acc, [str[0]]: str[1]}
}, {})

Create an empty String Buffer object. Traverse through the elements of the String array using loop. In the loop, append each element of the array to the StringBuffer object using the append () method. Finally convert the StringBuffer object to string using the toString () method.


It can be solve with split and a map, foreach item inside of the array replace the - with : using the split it will be turned into a key value, then assign those values to the object you want to return.

var arrayObject = ["gender-m", "age-20", "city-london", "lang-en", "support-home"];
var objectResulted ={};

arrayObject.map(
  function(item){ 
  var splitedValue = item.split('-');  
  objectResulted [splitedValue[0]]= splitedValue[1] 
  });

console.log(objectResulted);

In this tutorial, you will learn, how to convert strings to arrays & split string by a comma separated array in javascript. If you want to convert array or object array to comma separated string javascript or convert object array to a comma-separated string javascript.


Since you originally tagged this as a "Lodash" question, here is a Lodash-based answer:

const arr = ["gender-m", "age-20", "city-london", "lang-en", "support-home"];

const obj = _.chain(arr)
  .map(v => _.split(v, '-'))
  .fromPairs()
  .value();
  
console.log(obj);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

We have to create strings using character array. So to make some array of strings, we have to make a 2-dimentional array of characters. Each rows are holding different strings in that matrix. In C++ there is a class called string. Using this class object we can store string type data, and use them very efficiently. We can create array of


You can’t. Objects, strings, and arrays are three totally different concepts, and none of them can be “converted” to the others. Oh, they’re sometimes related, but they can’t be converted, no. Probably what you actually want is to write strings representing some of the contents or attributes of an object.


Since this is actually not a list of strings, the easiest way is to loop over it and convert each item into a new list of strings yourself: List<String> strings = list.stream() .map(object -> Objects.toString(object, null)) .collect(Collectors.toList()); Or when you're not on Java 8 yet:


String class split (String regex) can be used to convert String to array in java. If you are working with java regular expression, you can also use Pattern class split (String regex) method. Let’s see how to convert String to an array with a simple java class example.