0

i am working on object from which i want to remove a value on specific address. i need the position of element and want to remove it from object

here is the code :

var matches =  {
     "users-email": "email", 
     "users-activated": "activated", 
     "users-created_at": "created_at", 
     "users-registration_page": "registration_page", 
     "profiles-firstname": "firstname"
} ;
var option = 'firstname';

var if_in = $.inArray(option, matches );
//console.log(matches)
if( if_in !== -1 )
{
    matches.splice(if_in, 1);
}

$.each(matches , function(i,v){
   $('.test').append(v+'<br>');
});

in my object $.inArray() not working it return -1 .

can someone help me to find this issue .

jsfiddle link

6
  • 4
    your matches variable is an object, not an array. Commented Nov 13, 2014 at 16:39
  • @Alexander That will only work if option is the key. In this case it is the value Commented Nov 13, 2014 at 16:41
  • 2
    $.inArray only works on arrays. matches isn't an array. Commented Nov 13, 2014 at 16:44
  • @Haseeb - so youve used $.inArray previously, so what? In this case it wont do what you're hoping (expecting?) Commented Nov 13, 2014 at 16:44
  • 2
    $.inArray not worting on object No, it isn't, what makes you think it should? Commented Nov 13, 2014 at 16:47

3 Answers 3

4

Corrected answer to delete by value. You can use $.each to iterate over the object and delete the matching value.

$.each (matches, function(key, value) {
   if (value == option) {
      delete matches[key];
      return false; //deleted matching value
   }
});

to delete multiple keys by value,

var keysToDelete = [];
for (var key in matches) {
   if (matches[key] == option) {
      keysToDelete.push(key);
   }
};

for (var i = 0; i < keysToDelete.length; i++ ) {
   if (matches.hasOwnProperty(keysToDelete[i])) {
      delete matches[keysToDelete[i]];
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Because this doesn't solve the question. He's searching for a value, not key.
4

If you want to check if your option matches a value, simplest way would be to iterate through each property and check the values that way

var matches =  {"users-email": "email", "users-activated": "activated", "users-created_at": "created_at", "users-registration_page": "registration_page", "profiles-firstname": "firstname"} ;
var option = 'firstname';

for (var key in matches) {
    if (matches[key] === option) {
        alert ("IN");
        break;
    }
}

Comments

3

It seems like you're checking against the value and not the key; maybe something like this?

var key = undefined;
for(var i in matches) {
  if (matches[i] === option)  {key = i; break; }
}
if (key !== undefined) delete matches[key];

1 Comment

Might wanna break; after finding the key? :-P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.