我正在尝试使用表格数据制作一个对象
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"
}
]
我知道密钥名称不符合规则,但这些是列名。我可以访问第二列说创建 - > OFP 使用索引(例如cond[0][1]
)。
我只能访问cond[i]
如何访问内部块元素。
当我使用R时,json对象是使用jsonlite包创建的,所以,我有局限性。
答案 0 :(得分: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);
});

答案 1 :(得分:1)
据我了解,您希望能够做的是:
cond[0][1]; // "4.73"
你不能这样做,因为cond[0]
是和对象,并且不保证对象中键的顺序。因为Created ->OFP
是您输入的第二个键 并不意味着它实际上是对象中的第二个键。您可以查看Object.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
答案 2 :(得分: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]);
}
}