0

I have an object called allInvalidFields which lists invalid fields under an identifier e.g _0 or _3

The object could look like this

allInvalidFields = {
    "_0" : {
        0: input.foo,
        1: select.la
    }
    "_1" : {
        0: input.foofoo,
        1: select.lala
    }
}

But equally it could not have _0 as the first key and could look like this:

allInvalidFields = {
    "_1" : {
        0: input.alice,
        1: select.bob
    }
    "_3" : {
        0: input.foo
    }
}

How can I get the first value from the first object in the list? So in the example above it would be input.foo or input.alice depending on which dataset was being searched.

Nidhin David
  • 2,211
  • 2
  • 30
  • 37
user1486133
  • 1,084
  • 13
  • 26
  • 2
    What about sorting the keys and getting the first one? – BCartolo Feb 08 '17 at 15:45
  • 6
    The notion "first" explicitly does not make sense with JavaScript objects. Properties are in no particular order. If you need predictable order, use an array. – Pointy Feb 08 '17 at 15:45
  • @Pointy I am so stuck, because if I add to an array then I cannot use key and value pairs which I need e.g `_1` but if I add to an object then there is no first as you say. I'm totally lost. I have been trying to find a solution fr 5 days now. – user1486133 Feb 08 '17 at 15:49
  • This is not possible. Keys/properties/fields in a JavaScript Objects does not have any order. You cannot sort a javascript object as with an array.http://stackoverflow.com/a/5525820/3359432 – Nidhin David Feb 08 '17 at 15:50
  • Well you can impose your own ordering on the property names by keeping them in an adjunct array in whatever order makes sense for your purposes. – Pointy Feb 08 '17 at 15:50

3 Answers3

1

Inferred that the keys are in numeric order and prepended with "_".

Under these assumptions :

function first(obj,n){
  let smallest = Infinity;
  for(i in obj){
    console.log(i);
    let val='';
    if (i.toString()[0]=='_'){
      val = parseInt(i.toString().substring(1));
    }else{
      val = i;
    }
    if( val < smallest ){
      smallest = val;
    }
  }
  if(n>0){
     return first(obj["_"+smallest],n-1);
  }
  return obj[smallest];
}

first({"_0" : {
    0: "input.foo",
    1: "select.la"
},
"_1" : {
    0: "input.foofoo",
    1: "select.lala"
}},1); // input.foo
programaths
  • 868
  • 1
  • 11
  • 22
-1

You need something like this.

var allInvalidFields = {
  "_0": {
    "0": "input.foo",
    "1": "select.la"
  },
  "_1": {
    "0": "input.foofoo",
    "1": "select.lala"
  }
};

var firstInput = Object.keys(allInvalidFields)[0];
console.log(allInvalidFields[firstInput][0]);
VHS
  • 8,698
  • 3
  • 14
  • 38
-1
Object.values(allInvalidFields)[0][0];

take the first value of the first property object.

Small note: having such keys like _1 is bad style...

Short version of programaths answer:

allInvalidFields["_"+Object.keys(allInvalidFields).map(el=>el=+el.replace("_","")).sort()[0]][0]
Jonas Wilms
  • 106,571
  • 13
  • 98
  • 120
  • The problem is that there's no guarantee what that ordering will be. There'll definitely be a first value, but it may or may not be what the OP considers "first". – Pointy Feb 08 '17 at 15:57
  • If you pull first field, then it may not always the same as js objects does not implement order for its keys/fields http://www.stackoverflow.com/a/5525820/3359432 – Nidhin David Feb 08 '17 at 15:58
  • Missing depth flexibility of my answer ;-) – programaths Feb 08 '17 at 16:28