3

JavaScript

var myArray = [{key: "key1", value: "value1"},{key: "key1", value: "value1"}];

Is there an elegant mechanism for determining if "key1" is present ?

In other words, a better mechanism than iterating through all array elements and checking each one.

Edit:

(1) keys are not necessarily unique

(2) array must be composed of key-value pairs because I need to extract KVP subsets into other arrays , re-order , de-dupe , and many other complex operations

Please re-open this question, thanks.

BaltoStar
  • 8,165
  • 17
  • 59
  • 91
  • 2
    No, there isn't. Why don't you use an object with the keys as keys, instead of an array of objects? – Barmar Jul 01 '14 at 02:31
  • @Barmar - The key might not be unique, as shown in the example. – Derek 朕會功夫 Jul 01 '14 at 02:32
  • I'll bet it is, it's a key-value list. – Barmar Jul 01 '14 at 02:33
  • If you can't change the original array, but you need to do lots of tests like this, I suggest you make a new object that has all the keys as property names. Then use that in your code that tests whether the key is present. – Barmar Jul 01 '14 at 02:34
  • If you could explain the purpose, it would be better to think for this more logically. Thanks. – late_riser Jul 01 '14 at 02:58
  • Something has to iterate through all array elements and check each value; it's just a matter of which parts *you* have to implement :) – Ja͢ck Jul 01 '14 at 03:26

2 Answers2

2

If you're using an additional library that supports functional programming patterns, there are somewhat more elegant ways of expressing the iteration.

For instance, with Underscore:

_.some(myArray, function(elem) {return elem.key == 'key1';});

And cookie monster correctly points out that Underscore isn't necessary at all here, because some() is native on Array since 1.6:

myArray.some(function(elem) { return elem.key == 'key1';});
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
S McCrohan
  • 6,663
  • 1
  • 30
  • 39
  • 2
    +1 but you don't need libraries for that. `Array.prototype.some` is a native method. – cookie monster Jul 01 '14 at 02:47
  • Right you are! I've been so used to accessing it via underscore (because I'm already using it for other things) that I'd forgotten it's widely available natively now. Editing my answer with credit to you. – S McCrohan Jul 01 '14 at 02:51
1

In other words, a better mechanism than iterating through all array elements and checking each one.

Probably not, but you can filter on the collection easily:

var coll = [{key: 'key1', value: 'value1'}
           ,{key: 'key1', value: 'value1'}
           ,{key: 'key2', value: 'value1'}]

var values = function(x) {
  return Object.keys(x).map(function(k){return x[k]})
}

var result = coll.filter(function(x) {
  return values(x).indexOf('key1') > -1
})

console.log(result)
//^ [{key: 'key1', value: 'value1'}
//  ,{key: 'key1', value: 'value1'}]

The you can check if all the items have the value by comparing the lengths of the original collection and the filtered one:

if (result.length == coll.length) {
  // All objects have a key with value "key1"
}

Or you could use Array.prototype.every or Array.prototype.some depending on your needs.

elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • He's not explicit about it, but I'm pretty sure he's only interested in checking the value of the `key` property in each object. – cookie monster Jul 01 '14 at 02:50