Note that there are some explanatory texts on larger screens.

plurals
  1. POIn javascript, how would you build a method that compares value A with value B
    primarykey
    data
    text
    <p>I have an array of objects, something like this:</p> <pre><code>var myArray = [ { 'name' : 'some name', id: 23131, 'has' : ['dogs'] }, { 'name' : 'some name 2', id: 8678, 'has' : ['dogs', 'cats'] }, { 'name' : 'some name 3', id: 2125 , 'has' : ['donkeys', 'goats']}, { 'name' : 'some name 4', id: 90867, 'has' : ['parrots', 'treasure'] }, { 'name' : 'some name 5', id: 435458, 'has' : undefined }, ]; </code></pre> <p>And I want to retrieve specific elements that match certain criteria. For example, a person whose name contains number 5, and id is 435458. Or a person who has a parrot or a goat, or both.</p> <p>The method I'm trying to build takes two arguments, value A and value B. Value A is an object, like <code>{ 'name' : '5' }</code> or <code>{ 'name' : /5/ }</code> or <code>{ 'name' : 5 }</code>or { 'has' : 'goats' }, and value B is the object to match against, i.e. <code>myArray</code>.</p> <p>The method is quickly becoming quite complex and I feel that my code is not quite as effective and efficient as it could.</p> <p>I think the best way to achieve this is to loop through the objects and arrays that are passed and found (<code>myArray</code>, <code>has</code> array), and call it self until there only two string/number/regexp values to be compared against. But I'm not quite sure on how to best achieve this. Or is this not the best way to go? Also, speed is an important success criterion.</p> <p>Cheers</p> <p>Edit: <a href="http://jsbin.com/ediye/edit" rel="nofollow noreferrer">http://jsbin.com/ediye/edit</a> contains the function I'm using now, and I think it works as described above, but its quite slow for large data sets.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload