1

I have an array that looks like this

var Zips = [{Zip: 92880, Count:1}, {Zip:91710, Count:3}, {Zip:92672, Count:0}]

I would like to be able to access the Count property of a particular object via the Zip property so that I can increment the count when I get another zip that matches. I was hoping something like this but it's not quite right (This would be in a loop)

Zips[rows[i].Zipcode].Count

I know that's not right and am hoping that there is a solution without looping through the result set every time?

Thanks

4
  • What's wrong with it? Can you show your loop, and the rows object? Commented Aug 29, 2013 at 15:46
  • 1
    Zips[1].Zip would be 91710. Since your zipcodes in there are VALUES, not keys, you can't use them as a direct array lookup. You'd have to scan every object in that array and match against the zip values. Commented Aug 29, 2013 at 15:47
  • I don't know about zipcodes but is "0265" a valid zipcode? If so be careful when using them as Numbers you will lose the leading 0s. Commented Aug 29, 2013 at 15:48
  • each objects inside the {} is an object of Zips, so yeah - you don't need to loop, but you have to know the placement as what @MarcB pointed out. Commented Aug 29, 2013 at 15:49

6 Answers 6

5

I know that's not right and am hoping that there is a solution without looping through the result set every time?

No, you're gonna have to loop and find the appropriate value which meets your criteria. Alternatively you could use the filter method:

var filteredZips = Zips.filter(function(element) {
    return element.Zip == 92880;
});
if (filteredZips.length > 0) {
    // we have found a corresponding element
    var count = filteredZips[0].count;
}

If you had designed your object in a different manner:

var zips = {"92880": 1, "91710": 3, "92672": 0 };

then you could have directly accessed the Count:

var count = zips["92880"];
Sign up to request clarification or add additional context in comments.

2 Comments

I figured that was the case and am in the process of writing the loop but was hoping for a simpler solution. Oh well, thanks for the confirmation.
You could also use the filter method as shown in my answer which would avoid you the need of writing a loop. This is a native method that might be more optimized but don't expect an O(1) complexity.
1

In the current form, you can not access an element by its ZIP-code without a loop.

You could transform your array to an object of this form:

var Zips = { 92880: 1, 91710: 3 }; // etc.

Then you can access it by

Zips[rows[i].Zipcode]

To transform from array to object you could use this

var ZipsObj = {};
for( var i=Zips.length; i--; ) {
  ZipsObj[ Zips[i].Zip ] = Zips[i].Count;
}

Comments

1

Couple of mistakes in your code.

  1. Your array is collection of objects
  2. You can access objects with their property name and not property value i.e Zips[0]['Zip'] is correct, or by object notation Zips[0].Zip.

If you want to find the value you have to loop

Comments

1

If you want to keep the format of the array Zips and its elements

var Zips = [{Zip: 92880, Count:1}, {Zip:91710, Count:3}, {Zip:92672, Count:0}];
var MappedZips = {}; // first of all build hash by Zip
for (var i = 0; i < Zips.length; i++) {
    MappedZips[Zips[i].Zip] = Zips[i];
} 

MappedZips is {"92880": {Zip: 92880, Count:1}, "91710": {Zip:91710, Count:3}, "92672": {Zip:92672, Count:0}}

// then you can get Count by O(1)
alert(MappedZips[92880].Count);

// or can change data by O(1)
MappedZips[92880].Count++;
alert(MappedZips[92880].Count);

jsFiddle example

Comments

0
function getZip(zips, zipNumber) {
  var answer = null;

  zips.forEach(function(zip){
    if (zip.Zip === zipNumber) answer = zip;
  });

  return answer;
}

This function returns the zip object with the Zip property equal to zipNumber, or null if none exists.

Comments

-1

did you try this?

Zips[i].Zip.Count

1 Comment

Count is not a property of Zip.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.