0

I hope I am not repeating an existing question, I have tried really hard to find what I need on the site, but now feel I need to ask the question, so here goes, I hope you guys can help me out :s

I have an array;

var 1Results = somecontent;
var 2Results = somecontent;
var 3Results = somecontent;
var 4Results = somecontent;

var nomResults = 1Results + 2Results + 3Results + 4Results;

I have a script that is supposed to weed out the duplicate numbers and display them (in sorted_arr);

    var arr = nomResults;
    var sorted_arr = arr.sort(); // You can define the comparing function here. 
                                 // JS by default uses a crappy string compare.
    var results = [];
    for (var i = 0; i < arr.length - 1; i++) {
        if (sorted_arr[i + 1] == sorted_arr[i]) {
            results.push(sorted_arr[i]);
        }
    }

This script doesn't work, however is I change the script to this;

    var arr = [9, 9, 111, 2, 3, 4, 4, 5, 7];
    var sorted_arr = arr.sort(); // You can define the comparing function here. 
                                 // JS by default uses a crappy string compare.
    var results = [];
    for (var i = 0; i < arr.length - 1; i++) {
        if (sorted_arr[i + 1] == sorted_arr[i]) {
            results.push(sorted_arr[i]);
        }
    }

It works fine, any ideas why .sort() won't work with my pre popluated array?

Any help would be greatly appreciated.

4
  • 8
    var nomResults = 1Results + 2Results + 3Results + 4Results; is not an array and 1Results is not a valid JavaScript identifier. It should not even parse. Did you mean to use var nomResults = [Results1,Results2,...]? Commented Sep 4, 2014 at 16:19
  • Sorry, just edited the code to reflect additional arrays Commented Sep 4, 2014 at 16:21
  • 3
    Code is still showing that nomResults is just a concatenation of the other variables. It's still not an array. It is just a single value, so there is nothing to "sort." Your var arr IS an array (multiple comma-separated values inside square brackets), so it can sort. Commented Sep 4, 2014 at 16:26
  • In JavaScript identifier, the first character must be a letter, an underscore (_), or a dollar sign ($). It cannot be a digit. '+' operator cannot be used to manipulate or create arrays. This is array: [1,2,3]; this is not an array, it's a 6: 1+2+3 Commented Sep 4, 2014 at 16:26

3 Answers 3

1

You have to use brackets notation to add elements to your new Array like :

var arr = [Results1, Results2...];

Or Array.prototype.push() :

var arr = [];
arr.push(Results1);
arr.push(Results2);
//...

Plus you can use a specific function for sorting, either declaring a new function compare :

function compare(a, b) {
  return a - b;
}

Which will use actual values of your array (and not Strings, as specified in your comments). Then you pass it to sort :

arr = arr.sort(compare);

Or directly use anonymous function, if you don't need it more than one time :

arr = arr.sort(function(a, b) {
  return a - b;
});
Sign up to request clarification or add additional context in comments.

Comments

0

Two issues I can see in your code.

  1. The variable names must not begin with a number
  2. var nomResults is not an array but a string

The below code works for me just fine-

var results1 = "this", results2="is", results3="an", results4="array", theArray = [results1, results2, results3, results4];

console.log(theArray);

["this", "is", "an", "array"]

console.log(theArray.sort());

["an", "array", "is", "this"]

Comments

0

I was building my array wrong!!! Thanks guys, I knew it would be something simple :)

Instead of this;

var nomResults = 1Results + 2Results + 3Results + 4Results;

I needed to do this;

var nomResults = [Results1, Results2, Results3, Results4];

I've been looking at this for so long, I didn't see it. My script is all working now and great, errors are all gone, this is amazing. Many props and thanks to @jpreynat Thank you SO much :) I need a holiday....

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.