Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #16576

Re: Searching object using AND and OR

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail
From JJ <jaejunks_at@_googlemail_dot._com>
Newsgroups comp.lang.javascript
Subject Re: Searching object using AND and OR
Date Fri, 12 Oct 2012 13:25:59 +0000 (UTC)
Organization A noiseless patient Spider
Lines 65
Message-ID <XnsA0EAD0619B9DDjaejunksgooglemailco@0.0.0.13> (permalink)
References <ZaOdnXdCIZD3ZejNnZ2dnUVZ_vWdnZ2d@westnet.com.au>
Injection-Date Fri, 12 Oct 2012 13:25:59 +0000 (UTC)
Injection-Info mx04.eternal-september.org; posting-host="904c555971ca2171d99874e113a9e21d"; logging-data="21585"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+vhzvyjtruAAH/xrbNeWeOZKjKnFjbZ4w="
User-Agent Xnews/5.04.25
X-Face \*\`0(1j~VfYC>ebz[&O.]=,Nm\oRM{of,liRO#7Eqi4|!]!(Gs=Akgh{J)605>C9Air?pad{sSZ09u+A7f<^paR"/NH_#<mE1S"hde\c6PZLUB[t/s5-+Iu5DSc?P0+4%,Hl
Cancel-Lock sha1:LNaBQ3yMZ43XejbmW5Ja5jeILrE=
Xref csiph.com comp.lang.javascript:16576

Show key headers only | View raw


Andrew Poulos <ap_prog@hotmail.com> wrote:

> I've got a large (potentially hundreds of indexes) object that looks 
> something like
> 
> var obj = {
>    "a":["apple","banana"],
>    "b":["fish","apple","car","run","jump"],
>    "c":["car,"truck","motorcycle","banana"],
>    "d":["banana","boat","car","bicycle"],
>    ...
> };
> 
> Where array elements can only be individual words.
> 
> Its straightforward enough to go through the object and return all 
> indexes where a "search" word matches an element in the index's array 
> but I'm stuck trying to work out how to even approach allowing the use 
> of "AND" and "OR".
> 
> For example, to search for
>    banana AND (bicycle OR motorcycle)
> would return "c" and "d".
> 
> Andrew Poulos

Using your object format...

Example to case-sensitively search for:
banana AND (bicycle OR motorcycle)

var obj = {
  "a":["apple","banana"],
  "b":["fish","apple","car","run","jump"],
  "c":["car","truck","motorcycle","banana"],
  "d":["banana","boat","car","bicycle"]
};

var result = [];

//test each obj arrays
for (var i in obj) {
  //skip array if no banana
  if (obj[i].indexOf('banana') < 0) continue;
  //check for bicycle or motorcycle
  //in each array element
  for (var j = 0; j < obj[i].length; j++) {
    if (
      (obj[i][j] == 'bicycle') ||
      (obj[i][j] == 'motorcycle')
    ) {
      //found one. save result
      result.push(i);
      //no need to search further
      break;
    }
  }
}

console.log(result);
//shows: ["c", "d"]

//or...
console.log(result.join(''));
//shows: cd

Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Searching object using AND and OR Andrew Poulos <ap_prog@hotmail.com> - 2012-10-11 09:36 +1100
  Re: Searching object using AND and OR Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-11 01:13 +0200
  Re: Searching object using AND and OR Dr J R Stockton <reply1241@merlyn.demon.co.uk.invalid> - 2012-10-11 20:00 +0100
    Re: Searching object using AND and OR Gene Wirchenko <genew@ocis.net> - 2012-10-11 16:39 -0700
  Re: Searching object using AND and OR JJ <jaejunks_at@_googlemail_dot._com> - 2012-10-12 13:25 +0000

csiph-web