2

I have a problem with the creation of a multidimensional array in JavaScript.

The PHP code looks like this:

<?php
$matches = array(1, 2, 3, 4, 4, 4, 6, 6, 2, 3);

foreach($matches as $match) {

     $levels[$match][] = $match;
}

print_r($levels);
?>

print_r of $levels:

levels[1][0] = 1
levels[2][0] = 2
levels[3][0] = 3
levels[4][0] = 4
levels[4][1] = 4
levels[4][2] = 4
levels[6][0] = 6
levels[6][1] = 6
levels[2][1] = 2
levels[3][1] = 3

I have a problem with the creation the same array in JavaScript.

<script>
var levels = [];

$([1, 2, 3, 4, 4, 4, 6, 6, 2, 3]).each(function(key, value) {

     levels[value][] = value;
});
</script>

Can someone help me create the same array in JavaScript?

3 Answers 3

5

JavaScript doesn't magically create an array if there is none. You have to create it yourself. So it would be something like

$([1, 2, 3, 4, 4, 4, 6, 6, 2, 3]).each(function(key, value) {
    if (levels[value] == null) {
        levels[value] = [];
    }
    levels[value].push(value);
});

Learn more about arrays.

Sign up to request clarification or add additional context in comments.

3 Comments

Please use === instead of ==. Better yet, if(!levels[value])
@JanDvorak: If I used === it would not work, since the condition would never be fulfilled. x == null is just a short form for x === null || x === undefined. But yeah, simply converting to a boolean would be fine as well here.
Sorry... Never seen people actually using == null for x === null || x === undefined. I guess it's a reason I shouldn't be doing it either.
2

Felix's answer is good and readable, I just want to introduce you to a couple of useful idioms.

Unlike php, javascript || (boolean OR) operator, despite its name, doesn't return boolean, but rather the first non-falsy operand. So when you write

a = thing || otherThing

the result will be otherThing if thing is falsy (= null, undefined, 0, empty string). Therefore checks like this

if(!foo)
   foo = bar

can be written more concisely as

foo = foo || bar

If foo is truthy, it just assigns foo to foo (a no-op), otherwise, foo becomes bar.

Applied to your problem, this will look like this:

levels[value] = levels[value] || [];
levels[value].push(value);

If you want to shorten this even more, replace push with concat:

levels[value] = (levels[value] || []).concat(value);

This is not necessarily more readable or efficient, just something worth knowing about.

1 Comment

A solution in honor of your answer: [1, 2, 3, 4, 4, 4, 6, 6, 2, 3].reduce(function (p, c) { (p[c] || (p[c] = [])).push(c); return p; }, []);
0
var levels = [];
var orgArray = [1, 2, 3, 4, 4, 5, 6, 2];
for (var i = 0; i < orgArray.length; i++) {
    var value = orgArray[i];
    if (levels[value] === undefined)
        levels[value] = []
    levels[value].push(value);
}
//Print
for (var j = 0; j < levels.length; j++)
    if (levels[j])
        for (var k = 0; k < levels[j].length; k++)
            console.log("levels[" + j + "][" + k + "] = " + levels[j][k]);

It's a bit overkill but you will get the point I think. Don't forget to check if your elements exists

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.