1

I want to create an unsorted JavaScript array object for this I have two arrays

var degree_values = ['Bachelors', 'Doctoral / PhD', 'Masters', 'MBA', 'Professional Certifications'];
var degree_indexes = ["4", "3", "2", "1", "5"];
var values = {};     
for(var index in degree_values){
    values[degree_indexes[index]] = degree_values[index];
}
console.log(values);

The output is

Object { 1="MBA", 2="Masters", 3="Doctoral / PhD", 4 = 'MBA', 5 = 'Professional Certifications'}

The expected Output is

Object { 4 = 'MBA',  3="Doctoral / PhD", 2="Masters", 1="MBA", 5 = 'Professional Certifications'}
zavg
  • 10,351
  • 4
  • 44
  • 67
  • 1
    Objects do not maintain order of their properties. – zerkms Apr 03 '15 at 10:25
  • 3
    Your output and your expected output are identical, so everything's fine. – Tesseract Apr 03 '15 at 10:26
  • 2
    What's wrong? Your output *is* unsorted. – Bergi Apr 03 '15 at 10:30
  • I want unsorted output as shown in Expected output? –  Apr 03 '15 at 10:34
  • 1
    Oh, and [don't use `for in` loops on arrays](http://stackoverflow.com/q/500504/1048572)! – Bergi Apr 03 '15 at 10:47
  • 2
    If you expect the answer to be unsorted, then by definition you can't expect an order in the output. Equally, if you expect the resultant object to be in a particular order, by definition you want it sorted to that order. Can you clarify what unsorted means in the context you are using it? – Mike Curry Apr 03 '15 at 12:31

3 Answers3

0

Try changing your loop to for (i = 0; i < degree_values.length; i ++){ format and then access the variable using i as the position in the array.

As you cannot rely on the browser to maintain the object in order, as shown in the answer here How to keep an Javascript object/array ordered while also maintaining key lookups?, you can build a javascript object array and do as suggested there, which is to have an id and value for each element.

Here is your code with these changes:

var degree_values = ['Bachelors', 'Doctoral / PhD', 'Masters', 'MBA', 'Professional Certifications'];
var degree_indexes = ["4", "3", "2", "1", "5"];
var values = [];     
for (i = 0; i < degree_values.length; i ++){
    values[i] = {'id': degree_indexes[i], 'value' : degree_values[i]};
}
console.log(values);

And here is the output I got running this code:

VM1067:12 [Object, Object, Object, Object, Object]
0: Object
    id: "4"
    value:     "Bachelors"
    __proto__: Object
1: Object
    id: "3"
    value: "Doctoral / PhD"
    __proto__:     Object
2: Object
    id: "2"
    value: "Masters"
    __proto__: Object
3: Object
    id: "1"
    value:     "MBA"
    __proto__: Object
4: Object
    id: "5"
    value: "Professional Certifications"
    __proto__: Object
length: 5
__proto__: Array[0]
undefined
Community
  • 1
  • 1
Moishe Lipsker
  • 2,974
  • 2
  • 21
  • 29
0

Easy using the open source project jinqJs

See Fiddle

var degree_values = ['Bachelors', 'Doctoral / PhD', 'Masters', 'MBA', 'Professional Certifications'];
var degree_indexes = ["4", "3", "2", "1", "5"];

//Gets emails that are in current not in group
var result = jinqJs().from(degree_values).select(function(row,index){
    var obj = {};
    obj[degree_indexes[index]] = row;

    return obj;
});
NYTom
  • 524
  • 2
  • 14
0
Your output is: Object { 1="MBA", 2="Masters", 3="Doctoral / PhD", 4 = 'Bachelors', 5 = 'Professional Certifications'}
Expected : Object { 4 = 'MBA',  3="Masters", 2="Doctoral / PhD", 1="Bachelors", 5 = 'Professional Certifications'}

Since your keys are numbers, the browser will display it in order so what you finnaly want to get is (wich is the same but in order):

Object { 1: "Bachelors", 2: "Doctoral / PhD", 3: "Masters", 4: "MBA", 5: "Professional Certifications" }

in your for change the values not the keys (I just commented your line):

var degree_values = ['Bachelors', 'Doctoral / PhD', 'Masters', 'MBA', 'Professional Certifications'];
var degree_indexes = ["4", "3", "2", "1", "5"];
var values = {};

for(var index in degree_values) {
   // values[degree_indexes[index]] = degree_values[index];
   values[degree_indexes[index]] = degree_values[degree_indexes[index]-1]
}

Now you have the desired keys and values in your object. Then to display the properties in the order by degree_indexes, I used a high order function linked in its prototype:

degree_indexes.forEach(elm => {
   console.log(degree_values[elm-1]) /* 4, 3, 2, 1, 0*/
})

Output:
   MBA
   Masters
   Doctoral / PhD
   Bachelors
   Professional Certifications
vincent thorpe
  • 171
  • 1
  • 10