0

I have an object that contains things i'm looking for:

Exclusions

1: Object [ ID: "38433" , Location: "Central"] 2: Object [ ID: "43434" , Location: "West"]

and I have a Object called Schools that contains the set of all things: I am using the Object.keys() method to loop through the object, but I need to pass in what Location to search to search in say Central, or West,etc. If I try to do something like (Schools.District.location) I get an error. How do i pass in the correct argument to my object.keys function?

part2: What is the correct way to remove an element from that array. So i would need to remove Location[15] if say the ID's match.

Code:

$.each(Exclusions , function (index, value) {

var location = value.Location;  //gives me Central, West, East
var ID = value.ID;

Object.keys(Schools.District.{{pass in location}}).forEach(function (key) {

 //each location is an array of schools so i need to scan each for the matching ID


  for (var i=0; i < {{get length of location array}}.length; i++) {

      if(location[i][ID] == ID) 
       {
          location.slice();  //not sure about this
       }

    }


});
Edward
  • 3,061
  • 6
  • 32
  • 52
  • 1
    `Schools.District[location]` should work. http://jsfiddle.net/3aEK6/ – Will Feb 06 '14 at 07:47
  • 1
    and on deleting: **Array**: http://stackoverflow.com/questions/500606/javascript-array-delete-elements **Object.property:** http://stackoverflow.com/questions/208105/how-to-remove-a-property-from-a-javascript-object – Will Feb 06 '14 at 07:53
  • good answer @will to deleting a property on an object, thanks – Amir Sherafatian Feb 06 '14 at 08:15

2 Answers2

1

you want to use objects.key certainly ? why dont you use a for in ?

var result = [];
for (i in YOUR_OBJECT)
{
    if(YOUR_OBJECT[i].location == "what you want") result.push(YOUR_OBJECT[i]);
}

i guess you can not remove property you want probably, but you can set that to null, and at the end, get those property that its not null

EDIT: to delete a property on YOUR_OBJECT: (i is a key iterator)

delete YOUR_OBJECT[i]
Amir Sherafatian
  • 2,083
  • 2
  • 20
  • 32
1
  1. You should use @am1r_5h's answer above. For each iteration, i (I prefer to use key though) is a proper key. (In the case of an array, an integer index.) Collecting the keys to use later is not idiomatic JavaScript.

  2. Use the splice method to delete from an array. It completely deletes the element and replaces it with nothing so that the new length of the array is original_length - 1. Using delete and passing the element index will replace the element with undefined while the length of the array remains the same. Here's the docs for Array.prototype.slice: https://developer.mozilla.org/enUS/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

Yaw Boakye
  • 10,352
  • 1
  • 17
  • 25
  • are you sure i read that Object.keys() is the correct way to extract information in your array as it does not return prototype data. – Edward Feb 06 '14 at 08:02
  • Yeah, you're right. In the `for` loop, you could add this condition ```if (obj.hasOwnProperty(key)) { // code goes here ... } ``` to ensure that you're not messing with prototype data. – Yaw Boakye Feb 06 '14 at 08:06
  • @Yaw but i think @Edward has an object which to fill that properties by some locations and use that object as a dictionary, not an array... to deleting property on an object i guess there is no way but in an array using `.splice` like @Yaw said – Amir Sherafatian Feb 06 '14 at 08:12
  • good answer @will to deleting a property on an object, thanks – Amir Sherafatian Feb 06 '14 at 08:13
  • @am1r_5h `delete obj[key]`. That's how to delete a key-value pair from a JavaScript object – Yaw Boakye Feb 11 '14 at 06:07