0

I have two array with objects inside in it. I want to compare the values of a particular key from both arrays and do something with that. I have tried using inArray but couldn't succeed. Below is my code.

 function initialize() {
   geometry = 'http://yyyy.cartodb.com/api/v2/sql?q=SELECT name,ST_AsGeojson(the_geom) from msa_usa';
   $.getJSON(geometry,
     function(data) {
       var place_names = [];
       place_names.push({
         name: "Abilene",
         average: 8.65
       });
       for (i = 0; i < place_names.length; i++) {
         if ($.inArray(data.rows[i].name, place_names[i].name) > -1) {
           geom.push((data.rows[i].st_asgeojson));
           average_value.push(place_names[i].average);
         } else
           (console.log("else"));
         //console.log(place_names[i].name);
       }
     })
 }
 console.log(average_value.length);
 console.log(geom.length);
2
  • You can obviously use any indentation style you like in your own code, but when asking for help, please take the time to format the code in something like a standard, consistent way, perhaps with the help of a code beautifier. Making your code easily readable helps you get answers. Commented Dec 15, 2015 at 7:45
  • For future reference you should only post the code relevant for the question. Here only the one line is really interesting. An example of the actual datastructures would help make a more precise answer though. For the question, it is not what inArray is for, so if it is just a simple compare you should just do as jfriend00 wrote Commented Dec 15, 2015 at 7:47

1 Answer 1

1

I'm not sure why you're trying to use $.inArray() at all. It appears you're just trying to compare two properties on two objects (which happen to be in arrays, but you're already indexing to a particular object). If that's the case, then you can just directly compare the two properties:

if (data.rows[i].name === place_names[i].name)

But, in your code, you appear to have just created place_names so it will only have one value in it, not a whole bunch of values in it. So, now that confuses me as to what you're really trying to do here.

For more help, please describe in words what you're really trying to accomplish. Are you just trying to see if one particular .name property is in the data.rows array of objects?

If so, that would be a different piece of code like this:

function findPropInArray(array, propName, value) {
    for (var i = 0; i < array.length; i++) {
        if (array[i][propName] === value) {
            return i;
        }
    }
    return -1;
}

Then, in your code you could use something like this:

if (findPropInArray(data.rows, "name", "Abilene") !== -1) {
    // "Abilene" was as a name property in data.rows
}
Sign up to request clarification or add additional context in comments.

2 Comments

if (data.rows[i].name === place_names[i].name) this doesnot works since both are of different lengths.
@Harnish - Did you read the 2nd half of my answer? Your question and code do not describe well enough what you're trying to accomplish for me to know what else to suggest. If you want more help, please describe in words exactly what you're trying to do.

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.