0

I found out that there is no way to do this. However, thanks everyone! In javascript, can I override the brackets to access characters in a string?

I have something like this:

function f() {
    var list = [{name: 'test'}, {name: 'test2'}];
}

And I would like to get properties from that list that's inside the function, I think there is some method that overrides default [], but I really don't know what to look for.

f[0] // {name: 'test'}
f['test2'] // {name: 'test2'}
Community
  • 1
  • 1
Niccolò Campolungo
  • 11,824
  • 4
  • 32
  • 39

2 Answers2

1

Your list variable will be private to f() unless you do one of two things.

First, you could try returning list from f() so that you can then get to the properties you need.

function f() {
    var list = [{name: 'test'}, {name: 'test2'}];
    return list;
}

var f = f();
f[0] // {name: 'test'};
f['test'] // will return undefined; we'll come back to this

Second, and I think this option is probably what you're looking for as you tagged the question with 'oop', you could make f() a constructor:

function f() {
    this.list = [{name: 'test'}, {name: 'test2'}];
}

var f1 = new f();
f1['list'][0] // {name: 'test'};
f1.list[0] // will also return {name: 'test'};
f1.list['test'] // still will return undefined...

...

The reason you will not be able to access a value using ['test'] or ['test2'] is because they are your values, whereas ordinarily the values are what we want to access in an object using the key (in this case ['name'] or .name). So what you might want is something like this:

f1.list[0].name // will return 'test'
f1.list[1].name // will return 'test2'

Hope this clears up the confusion.

guypursey
  • 3,114
  • 3
  • 24
  • 42
  • I already knew these ways of solving my problem, but they are not exactly what I was looking for. Anyway, thank you, unfortunately I think it's impossible to solve in the way I thought. – Niccolò Campolungo Feb 17 '13 at 10:50
  • I'm writing a client that uses webSocket to interact with server. The problem is that i have a list of channels(it's similar to IRC) inside an object called ChannelList. That object has inside it a list channels = [] and some methods. Here is a piece of the code, if it can help: http://jsbin.com/isejuh/1/edit – Niccolò Campolungo Feb 17 '13 at 10:56
  • Okay, so was your question about how to access the `channels` array from outside of the ChannelList function? Or were you trying to rewrite ChannelList so it wasn't using the array but something else instead? – guypursey Feb 17 '13 at 11:09
  • I edited the question, it's impossible to override the [] accessor, I'll write a function to do what I want, thanks anyway! – Niccolò Campolungo Feb 17 '13 at 11:18
0

Just use an object instead of an array:

var byname = {
    "test": {name: 'test'},
    "test2": {name: 'test2'}
};
// bracket notation:
byname["test2"] // {name: 'test2'}
// dot notation:
byname.test // {name: 'test'}

Since you said that you never will access the structure by index, it will be OK to use the unordered key-value-map structure. To save typing, you could create that object dynamically from your array as well:

var list = [{name: 'test'}, {name: 'test2'}];

var byname = {};
for (var i=0; i<list.length; i++)
    byname[list[i].name] = list[i];
Bergi
  • 630,263
  • 148
  • 957
  • 1,375