-1

I am trying to make an object using table data

 var cond = [
        { 
    "locality": "Dwarka",
    "Created ->OFP": "4.73",
    "OFP -> Picked": "2.16",
    "Picked - > Delivery": "14.91",
    "Over_All_TAT": "22.65",
    "Total_Order": "159" 
  },
  {
    "locality": "IIT & AIIMS",
    "Created ->OFP": "2.56",
    "OFP -> Picked": "9.74",
    "Picked - > Delivery": "19.41",
    "Over_All_TAT": "32.61",
    "Total_Order": "54"
  },
  {
    "locality": "Gk1 & Nehru Place",
    "Created ->OFP": "2.39",
    "OFP -> Picked": "6.87",
    "Picked - > Delivery": "14.06",
    "Over_All_TAT": "24.13",
    "Total_Order": "31"
  },
  {
    "locality": "Mayur Vihar",
    "Created ->OFP": "3.52",
    "OFP -> Picked": "1.24",
    "Picked - > Delivery": "15.3",
    "Over_All_TAT": "21.03",
    "Total_Order": "33"
  },
  {
    "locality": "Snapdeal_Mundhka",
    "Created ->OFP": "17.32",
    "OFP -> Picked": "4.65",
    "Picked - > Delivery": "100.32",
    "Over_All_TAT": "123.16",
    "Total_Order": "31"
  }
  ]

I know the key names are not according to the rules , but those are the column names. Can i access the 2nd column say Created -> OFP using index (e.g. cond[0][1]).

I am able to access only cond[i] How can i access the inner block elements.

As I am using R , The json object is created using jsonlite package, So, I have limitations.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
Mukesh Kumar Singh
  • 623
  • 1
  • 10
  • 18
  • 6
    Like that : `cond[i]['Created ->OFP']` – R3tep Oct 01 '15 at 14:53
  • 5
    That isn't JSON its a plain JavaScript object. JSON is just a string format that represents an object. It would be parsed with `JSON.parse()`. – ste2425 Oct 01 '15 at 14:55
  • @Kay and Craicerjack: It looks like a dupe, but I don't believe they are actually asking the same thing. The OP wants to access the nested object using numeric indexes rather than the property names. – Matt Burland Oct 01 '15 at 15:49

3 Answers3

2

You can access on the inner block like this :

cond[i]['Created ->OFP']

Example with loop :

var cond = [{
  "locality": "Dwarka",
  "Created ->OFP": "4.73",
  "OFP -> Picked": "2.16",
  "Picked - > Delivery": "14.91",
  "Over_All_TAT": "22.65",
  "Total_Order": "159"
}, {
  "locality": "IIT & AIIMS",
  "Created ->OFP": "2.56",
  "OFP -> Picked": "9.74",
  "Picked - > Delivery": "19.41",
  "Over_All_TAT": "32.61",
  "Total_Order": "54"
}, {
  "locality": "Gk1 & Nehru Place",
  "Created ->OFP": "2.39",
  "OFP -> Picked": "6.87",
  "Picked - > Delivery": "14.06",
  "Over_All_TAT": "24.13",
  "Total_Order": "31"
}, {
  "locality": "Mayur Vihar",
  "Created ->OFP": "3.52",
  "OFP -> Picked": "1.24",
  "Picked - > Delivery": "15.3",
  "Over_All_TAT": "21.03",
  "Total_Order": "33"
}, {
  "locality": "Snapdeal_Mundhka",
  "Created ->OFP": "17.32",
  "OFP -> Picked": "4.65",
  "Picked - > Delivery": "100.32",
  "Over_All_TAT": "123.16",
  "Total_Order": "31"
}]
var val;
cond.forEach(function(el) {
  val = el['Created ->OFP'];
  console.log(val);
});
R3tep
  • 12,512
  • 10
  • 48
  • 75
1

As I understand it, what you'd like to be able to do is something like:

cond[0][1];   // "4.73"

You can't do that because cond[0] is and object and the order of keys in an object isn't guaranteed. So just because Created ->OFP is your second key as you entered it doesn't mean it's actually the second key in the object. You could look at Object.keys(cond[0]) and the keys will probably come back in the same order, but you cannot rely on this.

What it sounds like you need to do is transform the objects in your array (which isn't JSON as others have pointed out) into arrays themselves. So you end up with something like:

[
    [
        "Dwarka",
        "4.73",
        "2.16",
        "14.91",
        "22.65",
        "159"
    ],...
]

And then you could do:

cond[0][1];   //and get the first row and second column.

To transform your original into an array of arrays, you can do something like this:

// Note: this is the order **you** want the columns to be iterated in
var columns = ["locality",
    "Created ->OFP",
    "OFP -> Picked",
    "Picked - > Delivery",
    "Over_All_TAT",
    "Total_Order"];

var arrayOfArrays = cond.map(function (i) {
    return columns.map(function (prop) {
        return i[prop];
    });
});

console.log(arrayOfArrays[0][1]);   // logs 4.73

Fiddle

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
0

you can access your element like this :

cond[i]["Created ->OFP"]; // "4.73"

if you want to use indexes like 1, 2, you have two choices: using an array instead of objects, and it will look like this :

 var cond = [
        ["Dwarka", "4.73", "2.16", "14.91", "22.65", "159" ],
        [ ... ]
  }

or you can use the indexes 1, 2 ... on your object which will be like this:

var cond = [
        { 
    "0": "Dwarka",
    "1": "4.73",
    "2": "2.16",
    "3": "14.91",
    "4": "22.65",
    "5": "159" 
  },
  {...}
]

but you won't find the property length unless you add it as an element to your object.


I think the best solution for you, is to keep your object as it is, and use the loop for(key in cond)

for(var i=0; i<cond.length; i++) {
    for(var key in cond[i]) {
       console.log(cond[i][key]);
    }
}
Khalid
  • 4,730
  • 5
  • 27
  • 50
  • @R3tep I want to use column number . like 2 , 3 and say on – Mukesh Kumar Singh Oct 01 '15 at 14:56
  • 2
    @MukeshKumarSingh: You can't. The ordering of keys in a javascript object is not guaranteed. If you need to be able to access things by a numeric index, then you need an array. – Matt Burland Oct 01 '15 at 14:57
  • @MukeshKumarSingh, check updates – Khalid Oct 01 '15 at 15:03
  • @Khalid: The order that keys are iterated with `for...in` is not guaranteed. It will probably work most of the time, but it's very bad practice to rely on it. You are essentially tying yourself to a particular implementation of `for...in`. – Matt Burland Oct 01 '15 at 15:06