1

I stored an object in one variable (Consider as datatable).

var data=[{"controlID":"A","currentValue":"10","onChange":"","onClick":""},
        {"controlID":"B","currentValue":"5","onChange":"Testing(A,B)","onClick":""},
        {"controlID":"C","currentValue":"-5","onChange":"Testing1(A,B)","onClick":""},
        {"controlID":"D","currentValue":"","onChange":"Testing2(B,C)","onClick":""},{"controlID":"E","currentValue":"","onChange":"Testing3(C,D)","onClick":""},{"controlID":"F","currentValue":"","onChange":"","onClick":""}];

Now I know the second row key value as B. How to I Get the Third row (i.e., "C" row values)

Am new of this field. Please help us to helpful.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
ipln
  • 113
  • 1
  • 2
  • 14

2 Answers2

3

This function will return your index:

var FindIndexOfControlID = function(id, data){
    for(var i = 0; i < data.length ; i++){
        if( data[i]['controlID'] == id ){
            return i;
        }
    }
};

Usage:

var index = FindIndexOfControlID('C', data);

Live Example

http://jsfiddle.net/urahara/medhgm7b/


NOTE

Alternatively you may also want to implement function that returns index of any specified property and value:

var FindIndexOfProperty = function(value, property, data){
    for(var i = 0; i < data.length ; i++){
        if( data[i][property] == value ){
            return i;
        }
    }
}; 

Usage

FindIndexOfProperty('-5', 'currentValue',data); // returns 2
Piotr Dajlido
  • 1,982
  • 15
  • 28
  • Hi, I don't know 'B' th position. Then how to I give the data[2]. Or how to find the B'th row index. – ipln Mar 18 '15 at 10:19
  • Hi I need one more. i.e., var temp=Testing(A,B,C,D). I need A,B,C,D in different variable or in array list. Any predefined class or example is there..... – ipln Mar 18 '15 at 11:41
  • create new question I might answer it :) – Piotr Dajlido Mar 18 '15 at 11:42
  • please see my question "Get the Variable name inside the function in javascript" – ipln Mar 18 '15 at 11:47
0

You can return the third row in javascript by simply executing var thirdRow = data[2]. The row will be returned as an object.

callback
  • 3,981
  • 1
  • 31
  • 55