1

I have a string as follows : Panther^Pink,Green,Yellow|Dog^Hot,Top

This string means I have 2 main blocks(separated by a '|') : "Panther" and "Dog" Under these two main blocks, I have, lets say "subcategories".

I wanted to create a 2-dimensional array represented (in logic) as follows :

Panther(Array 1) => Pink(Element 1),Green(Element 2), Yellow(Element 3) Dog(Array 2) => Hot(Element 1), Top(Element 2)

Also,I want to be able to add a main block, lets say "Cat" with possible categories "Cute,Proud" to the two dimensional array

I've managed to get an Array containing "Panther^Pink,Green,Yellow" and "Dog^Hot,Top" by using JavaScript's split function.

Note that this string is received via Ajax and can be of any length, though the format shown above is always used.

----------------------------- EDIT ----------------------------

Ok, my script so far is :

    $(document).ready(function(){
        appFunc.setNoOfAppBlock('Panther^Pink,Green,Yellow|Dog^Hot,Top');
        appFunc.alertPing();
    });


    var appFunc = (function(stringWithSeper) {
        var result = {},
           i,
           categories = new Array(),
           subcategories;

        return {
            setNoOfAppBlock: function(stringWithSeper){
                categories = stringWithSeper.split("|");
                for (i = 0; i < categories.length; i++) {
                  subcategories = categories[i].split("^");
                  result[subcategories[0]] = subcategories[1].split(",");
                }
            },
            alertPing: function(){
                alert(result["Panther"][1]);
            }
        };
    })();

However, the function "alertPing" isn't "alerting" anything.What am am I doing wrong ?

4
  • Do you mean an associative array aka hash? Commented Dec 27, 2011 at 7:42
  • if the above can be achieved using an associative array, then, yes..though I prefer a simple two-dimensional array. Commented Dec 27, 2011 at 7:49
  • 1
    You're not describing a "simple two-dimensional array", you're describing an associative array made up of elements that are simple one-dimensional arrays. JavaScript's equivalent of an associative array is the "object", which is what I've used in my answer (and I see techfoobar has used an object too, beating me by a few seconds to post almost the same answer). If you use a simple two-dimensional array then you either lose the "Panther" and "Dog" keys, or you end up with them as the first element of each sub array - is that what you want? Commented Dec 27, 2011 at 8:06
  • @nnnnnn, that would do too, however,now,going through your solutions, I think I'd go about it the way you've show above ! Commented Dec 27, 2011 at 10:05

3 Answers 3

2

To me the most logical representation of your data:

Panther^Pink,Green,Yellow|Dog^Hot,Top

Is with a JavaScript object with a property for each category, each of which is an array with the subcategories:

var data = {
   Panther : ["Pink", "Green", "Yellow"],
   Dog     : ["Hot", "Top"]
}

You would then access that by saying, e.g., data["Dog"][1] (gives "Top").

If that format is acceptable to you then you could parse it as follows:

function parseData(data) {
   var result = {},
       i,
       categories = data.split("|"),
       subcategories;

   for (i = 0; i < categories.length; i++) {
      subcategories = categories[i].split("^");
      result[subcategories[0]] = subcategories[1].split(",");
   }

   return result;
}

var str = "Panther^Pink,Green,Yellow|Dog^Hot,Top";
var data = parseData(str);
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you're trying to parse your data into something like this:

var result = {
   Panther: ["Pink", "Green", "Yellow"],
   Dog: ["Hot", "Top"]
}

you can use string.split() to break up your string into subarrays:

var str = "Panther^Pink,Green,Yellow|Dog^Hot,Top";
var result = {}, temp;
var blocks = str.split("|");
for (var i = 0; i < blocks.length; i++) {
    temp = blocks[i].split("^");
    result[temp[0]] = temp[1].split(",");
}

Data can then be added to that data structure like this:

result["Cat"] = ["Cute", "Proud"];

Data can be read from that data structure like this:

var dogItems = result["Dog"];    // gives you an array ["Hot", "Top"]

Comments

1

You can use something like:

function parseInput(_input) {
  var output = [];
  var parts = _input.split('|');
  var part;
  for(var i=0; i<parts.length; i++) {
    part = parts[i].split('^');
    output[part[0]] = part[1].split(',');
  }
  return output; 
}

Calling parseInput('Panther^Pink,Green,Yellow|Dog^Hot,Top'); will return:

output [
 "Panther" => [ "Pink", "Green", "Yellow" ],
 "Dog" => [ "Hot", "Top" ]
]

To add another item to the list, you can use:

output["Cat"] = ["Cute", "Proud"];

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.