Question: sorry for my bad english 

i am working in javascript and i have build an array i want to enter values in this array.

```
var attribute_sets = [];
$('.attribute_set :selected').each(function(i, selected){
    attribute_sets[i]['id'] = $(selected).val(); // getting id 
    attribute_sets[i]['name'] = $(selected).text(); // getting name
});

```

Its giving me error 

> 
> TypeError: attribute\_sets[i] is undefined
> 
> 
> 

also tried this one 

```
attribute_sets[i]['id'].push($wk_jq(selected).val());

```

still getting same error

can any one please guide me how can i insert values in this JS array.
i want output like this 

```
array
    [0]
      'id':'1',
      'name':'abc'
    [1]
      'id':'2',
      'name':'xyz'

```

Answer: 
I would recommend an extension like this:

```
extension NSMutableArray {
public func appendWithSuffix(strNewEntry:String) {
    var n = 1
    var new = strNewEntry
    if self.containsObject(strNewEntry) {
        new = strNewEntry + "_\(n)"
        while self.containsObject( new ){
            n += 1
            new = strNewEntry + "_\(n)"
        }
    }
    self.addObject( new )
}}

```

It will look for an exact match and insert if none is found, i.e it will add "Ryan\_5" instead of "Ryan", "John\_3" instead of "John" and so on.

Another approach may be to iterate thru all entries for comparison or filter the array with `"namexy".hasPrefix("Ryan")` to get max index of name to be inserted.
```
- (IBAction)SaveText:(id)sender
{

if (array.count==0)
    {
        [array addObject:_txtAddText.text];
    }else
    {

        for(NSString *string in array)
        {
            if ([string containsString:_txtAddText.text] )
            {
                count = count+1;
              NSString *Localstring =[NSString stringWithFormat:@"%@%d",[string stringByAppendingString:@"_"],count];
                 NSLog(@"%@",Localstring);
                [array addObject:Localstring];
                break;
            }else
            {
                [array addObject:_txtAddText.text];
            }
        }

    }

    NSLog(@"%@",array);

}

```