admin管理员组

文章数量:1353653

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

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

Share Improve this question edited Aug 29, 2013 at 15:47 user229044 240k41 gold badges344 silver badges346 bronze badges asked Aug 29, 2013 at 15:45 user1735894user1735894 3234 silver badges17 bronze badges 4
  • What's wrong with it? Can you show your loop, and the rows object? – CodingIntrigue 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. – Marc B 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. – Halcyon 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. – peterchon Commented Aug 29, 2013 at 15:49
Add a ment  | 

6 Answers 6

Reset to default 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"];

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;
}

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

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

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.

did you try this?

Zips[i].Zip.Count

本文标签: access javascript array element by JSON object keyStack Overflow