0

I have this array:

["Name: Bob, LastName: Smith, Age: 77","Name: John, LastName: Cabbage, Age: 34","Name: Regan, LastName: Gobsmolly, Age: 80"]

How would you split all the strings in the array into more arrays from the comma, so the result would end up like this:

[["Name: Bob", "LastName: Smith", "Age: 77"],["Name: John", "LastName: Cabbage", "Age: 34"],["Name: Regan", "LastName: Gobsmolly", "Age: 80"]]

I have already tried splitting the items in the array with a loop, with the aim of returning the Name value from every array:

function getEntryNames(nameSplit) {
        var nameSplit;
        for (var i = 0; i < nameSplit.length; i++) {
            return nameSplit[i].split(',')[i];
        }
    }
document.write(getEntryNames(theArray))

The problem is, the function will only split the first item in the array, so I get this result:

Name: Bob

When I really want this result:

Name: Bob, Name: John, Name: Regan

So how do you split items in an array to more arrays?

5 Answers 5

1

What about this: (the original array content will be replaced in the process)

var arr = ["Name: Bob, LastName: Smith, Age: 77","Name: John, LastName: Cabbage, Age: 34","Name: Regan, LastName: Gobsmolly, Age: 80"];

function splitSomething(arr) {
  var item;
  for (var i = 0; i < arr.length; i++)
    {
      item = arr[i];
      arr[i] = item.split(',');
    }
  return arr;
}
console.log(splitSomething(arr));
Sign up to request clarification or add additional context in comments.

Comments

1

You can simply execute split() to every item of an array using Array.prototype.map:

var arr = ["Name: Bob, LastName: Smith, Age: 77","Name: John, LastName: Cabbage, Age: 34","Name: Regan, LastName: Gobsmolly, Age: 80"];

arr = arr.map(function(x) { 
    return x.split(',').map(function(x) { return x.trim(); });
});

document.body.innerHTML = JSON.stringify(arr); // just an output

String.prototype.trim is simply supposed to remove leading and trailing spaces.

1 Comment

Thank you for informing me about map() (I am relatively new to javascript)
1

Assuming that your array name is arr:

arr.map(function (e) { return e.split(", "); })

I tested this code in nodejs console, and the result is:

> var arr = ["Name: Bob, LastName: Smith, Age: 77","Name: John, LastName: Cabbage, Age: 34","Name: Regan, LastName: Gobsmolly, Age: 80"]
undefined
> arr.map(function (e) { return e.split(", "); })
[ [ 'Name: Bob', 'LastName: Smith', 'Age: 77' ],
  [ 'Name: John', 'LastName: Cabbage', 'Age: 34' ],
  [ 'Name: Regan', 'LastName: Gobsmolly', 'Age: 80' ] ]

Comments

0

Array.map can do this easily :

arr.map(function(str) {
    return str.split(',');
});

You may have to trim some strings to have the good result :

arr.map(function(str) {
    return str.split(',').map(function(val) {
        return val.trim();
    });
});

Comments

0

Should be something like this. Because you put return so the function after the first element.

function getData(data) {
  for (var i = 0; i < data.length; i++) {
    var value = data[i];
    var item = {};
    var array = value.split(",");
    for (var j = 0; j < array.length; j++) {
      var element = array[j].split(":");
      var key = element[0].trim();
      var value = element[1].trim();
      item[key] = value;
    }
    result.push(item);
  }
  return result;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.