1

I am a JS newbie and having a hard time figuring out how to run a function with what I have..

The function:

function compressArray(original) {

    var compressed = [];
    // make a copy of the input array
    var copy = original.slice(0);

    // first loop goes over every element
    for (var i = 0; i < original.length; i++) {

        var myCount = 0;    
        // loop over every element in the copy and see if it's the same
        for (var w = 0; w < copy.length; w++) {
            if (original[i] == copy[w]) {
                // increase amount of times duplicate is found
                myCount++;
                // sets item to undefined
                delete copy[w];
            }
        }

        if (myCount > 0) {
            var a = new Object();
            a.value = original[i];
            a.count = myCount;
            compressed.push(a);
        }
    }

    return compressed;
};

I have a multidimensional array like below that I want to pull out the third element to run through the function.

var animalcount = [
        [2.8, 20, "dog, cat, bird, dog, dog"],
        [4.2, 22, "hippo, panda, giraffe, panda"],
        [3.7, 41, "snake, alligator, tiger, tiger"]
                ];

So I'm trying to figure out how to get the array to be single arrays like below

var newArray1 = ("dog", "cat", "bird", "dog", dog");
var newArray2 = ("hippo", "panda", "giraffe", "panda");

or ideally tweak the function so that the multidimensional array can stay in tact.

1
  • It is not clear at all what you're trying to do or what the code you included has to do with your question. Commented Oct 17, 2013 at 18:07

2 Answers 2

2

This is kinda too localized but something like this.

var newArray1 = animalcount[0][2].split(', ');
var newArray2 = animalcount[1][2].split(', ');
Sign up to request clarification or add additional context in comments.

1 Comment

It did, didn't get it by the 3rd, but pop gets the last item in the array
0
// Your input array
var animalcount = [
    [2.8, 20, "dog, cat, bird, dog, dog"],
    [4.2, 22, "hippo, panda, giraffe, panda"],
    [3.7, 41, "snake, alligator, tiger, tiger"]
];

// Your empty output array
var results = [];

// For every record in your input array
for(var i = 0; i < animalcount.length; i++){
    // Get the string list, split it on the commas, and store the
    // result in your output array at the same index
    results[i] = animalcount[i][2].split(", ");
}

With the above code, your output would look like the following:

results = [
    ["dog", "cat", "bird", "dog", "dog"],
    ["hippo", "panda", "giraffe", "panda"],
    ["snake", "alligator", "tiger", "tiger"]
];

1 Comment

Thanks for your help! Sorry - I wasn't clear enough before. I was able to get that result before with split but it doesn't work with the function, which gives me a count of 1 for each array. What I'm trying to do is count the number of elements using the function above, so if there's a better way to that, even better. I have been able to display the animals using d3, but what I'd like to do is count them so that for example, "dog" would be in larger font of the repetition, very much like wordle, except I only want different font sizes, not layouts.

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.