1

I am a bit of a novice here, so I will just jump right into the example of the issue I am having:

I want to convert this: "cat@dog%arm@hand%tree@apple..."

into this:

cat | dog
arm | hand
tree| apple
..etc

As in, 2 columns in the spreadsheet, or a 2D array. Isolating the strings using has not been difficult, it is the array manipulation that is stumping me.

Here is one attempt: `

function key_abb(input) {
  if(input.map) { 
    return input.map(key_abb);
  }else {
    var temp = input.toString();
    temp = temp.replace("abbreviated-name=", "");
    temp = temp.replace("name=", "");
    return temp;
  }
}`

input being an input range that is formatted like this:

|abbreviated-name="cat"  name="dog"  |
|abbreviated-name="arm"  name="hand" |...

Unfortunately this is still just returning the two strings within the same column, so the only thing that was accomplished was that the extra text was removed, I am not sure how to create the output array that I would like. Thank you!

2
  • You will find value in the .split() method. Commented Mar 8, 2016 at 1:43
  • Hi marty, I have also been trying that, but I am still stuck with a similar problem: How do I get the 1D array sorted into a 2D output array? Also, how do I do 'code' in the comments? Commented Mar 8, 2016 at 1:45

2 Answers 2

2

Here you go:

var str = 'cat@dog%arm@hand%tree@apple';
var data = str.split('%');

for (var i = 0; i < data.length; i++) {
    data[i] = data[i].split('@');
}
    
console.log(data);

Results in:

[
    ["cat", "dog"],
    ["arm", "hand"],
    ["tree", "apple"]
]
Sign up to request clarification or add additional context in comments.

1 Comment

Wow that totally worked, I really just have a difficult time grasping arrays and manipulating them. Thank you so much!
1

Here's another option using Array.map():

var str = 'cat@dog%arm@hand%tree@apple';
var data = str.split('%').map(function(item) {
  return item.split('@');
});
    
console.log(data);

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.