0

Hello all,

I would like your help on chunking a multidimensional array, which appears to be a complex job. The array has the following structure:

name["rows"]["key1"] = some String;
name["rows"]["key2"] = some String;
name["rows"]["key3"] = some String;
name["rows"]["key4"] = some String;
name["rows"]["key5"] = some String;
...
name["rows"]["keyN"] = some String;

What I want to achieve is to have another master array containing the various chunks:

masterArray["rows"]["key1"]... etc.

It would be nice to choose how many arrays can be included in the master array.

Any thoughts around it please?

Any help will be highly appreciated!

Thank you in advance!!!

2
Contributors
5
Replies
7
Views
6 Years
Discussion Span
Last Post by Taywin
0

Hmm... You can do it that way in Javascript because the script is a weak type language. You could simply do like...

var multiDArray = new Array();
// then either push the array or assign it directly
multiDArray.push(element1)  // add the element to the end of array [element1]
multiDArray[3] = element2   // create [element1, undefined, undefined, element2]

// if you want a hash, use object or {}
var obj = new Object()
obj["row"] = new Object()
obj["row"]["key1"] = aValue
multiDArray.push(obj)  // create [{"row"=>{"key1"=>aValue}}]

Edited by Taywin: n/a

0

Hey Taywin, thanks for your reply! Is there a quick way to chunk the arrays as well? Meaning: Let's say that we have an array with 1000 keys: name["rows"]["key1000"] = some String.

How can I chunk them into batches of, 50 arrays? So each masterArray["rows"]["key1"] will have a maximum number of 50 ["rows"]["keys"].

Many thanks!

Hmm... You can do it that way in Javascript because the script is a weak type language. You could simply do like...

var multiDArray = new Array();
// then either push the array or assign it directly
multiDArray.push(element1)  // add the element to the end of array [element1]
multiDArray[3] = element2   // create [element1, undefined, undefined, element2]

// if you want a hash, use object or {}
var obj = new Object()
obj["row"] = new Object()
obj["row"]["key1"] = aValue
multiDArray.push(obj)  // create [{"row"=>{"key1"=>aValue}}]
0

You could do something like...

var masterArray = new Array()
for (var i=0; i<50; i++) {
  // create the object for "rows" and "key#" here...
  masterArray[i] = new Object()
  masterArray[i]["rows"] = new Object()

  // now go through another loop to create
  // masterArray[i]["rows"]["key1"] ~ masterArray[i]["rows"]["key20"]
  for (var j=1; j<=20; j++) {
    masterArray[i]["rows"]["key"+j] = aValue
  }
}
0

Hey Taywin, very good solution, thanks very much for that. What happens when the keys are random works instead of keyN? how do I get the first i.e. 200 keys from an associative array? thanks!

You could do something like...

var masterArray = new Array()
for (var i=0; i<50; i++) {
  // create the object for "rows" and "key#" here...
  masterArray[i] = new Object()
  masterArray[i]["rows"] = new Object()

  // now go through another loop to create
  // masterArray[i]["rows"]["key1"] ~ masterArray[i]["rows"]["key20"]
  for (var j=1; j<=20; j++) {
    masterArray[i]["rows"]["key"+j] = aValue
  }
}
0

That's the weak part of JavaScript. They provide the part for hash but not a complete service. You could either implement your own Hash object or have an array declaring outside the scope to keep track of all keys when you have already added a value to them.

var keys = new Array()
  for (var j=1; j<=20; j++) {
    masterArray[i]["rows"]["key"+j] = aValue
    keys.push("key"+j)
  }

// then when you search for the key existent
function keyIndexOf(key) {
  for (var i=keys.length-1; i>=0; i--) {
    if (key==keys[i]) { return i; }
  }
  return -1;  // not found
}

You would have to implement the search inside your array and cannot use the indexOf() because the function is not implemented in IE. In other words, other browsers has the function built-in for JavaScript array but IE doesn't. Still, the function I gave you above should work fine with regular key string.

Edited by Taywin: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.