0

There are two arrays:

itemKeys: [
{
    name: "REFOBJTYPE"
},
{
    name: "REFOBJKEY"
}
...
]

itemValues: [
{
    value: ""
},
{
    value: ""
}
]

and an object

ref: {
    REFOBJTYPE: 1,
    REFOBJKEY: 2, 
}

They are fixed and the structure itself cannot be changed.

values of itemValues should be filled with values from ref object, to get index we have to look up the itemKeys array.

The point of this question: I don't want to use 2 "for" loops to check for each key if it exists in ref. I WANT use JAVASCRIPT specific features like maybe "indexOf", so:

is the ANY OTHER way, rather than TWO FOR-LOOPs to complete this task?

Please don't question why I need this, why don't I like 2 loops. Obviously under any implementation "behind" it will be 2 loops.

Max
  • 1,463
  • 5
  • 19
  • 34
  • 1
    *" I don't want to use 2 "for" loops to check for each key if it exists in ref"* You just have to iterate over `itemKeys` and can check the existing with `ref[obj.name]`. No need for two loops. Or am I missing something? – Felix Kling Jun 24 '13 at 08:35
  • @FelixKling good point! let me check this with the scenario. Thanks! – Max Jun 24 '13 at 08:37
  • @FelixKling ok, let's say switching from C# to JS brought again some stupid obvious questions :) – Max Jun 24 '13 at 08:47

1 Answers1

0

I'm not sure if this is what you meant, but if you loop over the itemKeys array, you can easily look up the value associated with the key in the ref object, and then write that out to the itemValues array in one loop.

for (var i = 0; i < itemKeys.length; i++) {
  var key = itemKeys[i].name;
  var value = ref[key];
  itemValues[i].value = value;
}
James Holderness
  • 22,721
  • 2
  • 40
  • 52