-1

我正在尝试使用表数据制作对象

 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"
  }
  ]

我知道键名不符合规则,但那些是列名。我可以使用索引(例如cond[0][1])访问第二列,例如 Created -> OFP。

我只能访问cond[i]如何访问内部块元素。

当我使用 R 时,json 对象是使用 jsonlite 包创建的,所以,我有限制。

4

3 回答 3

2

您可以像这样访问内部块:

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

循环示例:

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

于 2015-10-01T14:55:06.713 回答
1

据我了解,您希望能够做的是:

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

您不能这样做,因为cond[0]无法保证 is 和 object 以及对象中键的顺序。因此,仅仅因为您输入Created ->OFP的第二个键并不意味着它实际上是对象中的第二个键。您可以查看并且密钥可能会以相同的顺序返回,但您不能依赖 thisObject.keys(cond[0])

听起来您需要做的是将数组中的对象(不是其他人指出的 JSON)转换为数组本身。所以你最终会得到类似的东西:

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

然后你可以这样做:

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

要将原始数据转换为数组数组,您可以执行以下操作:

// 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

小提琴

于 2015-10-01T15:05:00.693 回答
0

您可以像这样访问您的元素:

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

如果你想使用像 1、2 这样的索引,你有两个选择:使用数组而不是对象,它看起来像这样:

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

或者您可以在对象上使用索引 1、2 ...,如下所示:

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

length但是除非将其作为元素添加到对象中,否则您将找不到该属性。


我认为对你来说最好的解决方案是保持你的对象不变,并使用循环for(key in cond)

for(var i=0; i<cond.length; i++) {
    for(var key in cond[i]) {
       console.log(cond[i][key]);
    }
}
于 2015-10-01T14:54:54.123 回答