1

Here is a small example:

var distinctValues = [];
distinctValues.push("Value1");
distinctValues.push("Value2");

var firstValue = distinctValues[0];

var searchResults = [];

var data = grid.jqGrid('getGridParam', 'data');
data.forEach(function (row) {

  searchResults[searchResults.length] =
  {
    "ID"       : row.ID,
    "CreatedBy": row.CreatedBy,
    "UpdatedBy": row.UpdatedBy
  }
}

How do I look firstValue("Value1") inside searchResults array and retrieve the CreatedBy information?

//something like this - this is wrong syntax by the way
if ($.inArray(firstValue, searchResults) != -1) {
      alert(searchResults["CreatedBy"]);
}
0

1 Answer 1

1

I think you may want to do this:

var searchResults = [];
data.forEach(function (row) {

  searchResults.push({ //Push to push the record to the array
    "ID"       : row.ID,
    "CreatedBy": row.CreatedBy,
    "UpdatedBy": row.UpdatedBy
  });
}

You can use jquery $.inArray or Array.prototype.indexOf

searchResults is an array of objects so use indexer i.e searchResults[index]["CreatedBy"] instead of searchResults["CreatedBy"]:

var idx = searchResults.indexOf(firstValue); //or var idx = $.inArray(firstValue, searchResults)
if (idx  > -1) {
      alert(searchResults[idx]["CreatedBy"]);  //item Found
}

Nothing wrong with your syntax for $.inArray, Provided you have jquery included in your code.

Since your match is against an object property you can try this:

   var result = $.grep(searchResults, function(value)
   {
       return value.ID === firstValue;
   });

  console.log(result[0].CreatedBy); //result will be an array of matches.
Sign up to request clarification or add additional context in comments.

10 Comments

we can use arr[arr.length] for adding elements .. but arr.push is more fastest
Unfortunately that always returns -1. I debugged it and can see that the value exists in the array :-(
@Max How does your array look and value? Definitely has to be some issue with the data only... can you a console.log of your array and the data you are trying to compare. We will try it in a fiddle.
I also tried: var arr = $.grep(searchJobInfoResults, function (n, i) { return (n == value); });
Here is it: TraceInfo("Value: " + value); TraceInfo("1st element: " + searchJobInfoResults[0]["GroupInstanceID"]); TraceInfo("1st element: " + searchJobInfoResults[0]["CreatedBy"]); Value: e6cc8074-d51f-4038-bc97-a3bd22e53bc7 1st element: e6cc8074-d51f-4038-bc97-a3bd22e53bc7 1st element: DOMAIN01\JOHNB
|

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.