Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #16546 > unrolled thread
| Started by | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| First post | 2012-10-11 09:36 +1100 |
| Last post | 2012-10-12 13:25 +0000 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.javascript
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
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2012-10-11 09:36 +1100 |
| Subject | Searching object using AND and OR |
| Message-ID | <ZaOdnXdCIZD3ZejNnZ2dnUVZ_vWdnZ2d@westnet.com.au> |
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
[toc] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2012-10-11 01:13 +0200 |
| Message-ID | <1523166.s9Bo9gtfRQ@PointedEars.de> |
| In reply to | #16546 |
Andrew Poulos 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".
Since you are looking for the property names, based on the words, ISTM you
should transform `obj' into an index object that is referred by
{
"apple": ["a", "b"],
"banana" ["a", "c", "d"],
"bicycle": ["d"],
"boat": ["d"],
"car": ["b", "c", "d"],
"fish": ["b"],
"jump": ["b"],
"motorcycle": ["c"],
"run": ["b"],
"truck": ["c"],
...
};
Then, applying knowledge about the equivalence of boolean and set
operations, the solution should present itself.
HTH
PointedEars
--
When all you know is jQuery, every problem looks $(olvable).
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1241@merlyn.demon.co.uk.invalid> |
|---|---|
| Date | 2012-10-11 20:00 +0100 |
| Message-ID | <+fen$YOpdxdQFwkd@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #16546 |
In comp.lang.javascript message <ZaOdnXdCIZD3ZejNnZ2dnUVZ_vWdnZ2d@westne
t.com.au>, Thu, 11 Oct 2012 09:36:13, Andrew Poulos
<ap_prog@hotmail.com> posted:
>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".
This OBJ looks something like your obj (and can be made from it)
var OBJ = {
"a":{"apple":1,"banana":1},
"b":{"fish":1,"apple":1,"car":1,"run":1,"jump":1},
"c":{"car":1,"truck":1,"motorcycle":1,"banana":1},
"d":{"banana":1,"boat":1,"car":1,"bicycle":1}};
S = ""
for (Z in OBJ) {
El = OBJ[Z]
if (El["banana"] && (El["bicycle"] || El["motorcycle"]) ) S += Z }
S // now cd <g>
--
(c) John Stockton, nr London UK Reply address via Home Page.
news:comp.lang.javascript FAQ <http://www.jibbering.com/faq/index.html>.
<http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
[toc] | [prev] | [next] | [standalone]
| From | Gene Wirchenko <genew@ocis.net> |
|---|---|
| Date | 2012-10-11 16:39 -0700 |
| Message-ID | <c4me78t9n3uurubajpm50db3r0v0s5pfot@4ax.com> |
| In reply to | #16566 |
On Thu, 11 Oct 2012 20:00:57 +0100, Dr J R Stockton
<reply1241@merlyn.demon.co.uk.invalid> wrote:
[snip]
>This OBJ looks something like your obj (and can be made from it)
>
>var OBJ = {
> "a":{"apple":1,"banana":1},
> "b":{"fish":1,"apple":1,"car":1,"run":1,"jump":1},
> "c":{"car":1,"truck":1,"motorcycle":1,"banana":1},
> "d":{"banana":1,"boat":1,"car":1,"bicycle":1}};
>
>S = ""
>for (Z in OBJ) {
> El = OBJ[Z]
> if (El["banana"] && (El["bicycle"] || El["motorcycle"]) ) S += Z }
>S // now cd <g>
Nice! Thank you for the lesson.
Sincerely,
Gene Wirchenko
[toc] | [prev] | [next] | [standalone]
| From | JJ <jaejunks_at@_googlemail_dot._com> |
|---|---|
| Date | 2012-10-12 13:25 +0000 |
| Message-ID | <XnsA0EAD0619B9DDjaejunksgooglemailco@0.0.0.13> |
| In reply to | #16546 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web