Javascript 确定数组元素是表示对象还是表示值
从JSON对象(包含股票数据)中,我想向数组中添加某些元素(在Google Sheets脚本编辑器中): 以下是我目前仅读取某些特定值的方法:Javascript 确定数组元素是表示对象还是表示值,javascript,Javascript,从JSON对象(包含股票数据)中,我想向数组中添加某些元素(在Google Sheets脚本编辑器中): 以下是我目前仅读取某些特定值的方法: var arr = []; arr.push(quote.quoteSummary.result[0].Profile.sector); // Technology arr.push(quote.quoteSummary.result[0].Owners[1].name); // Susan arr.push(quote.quoteSummary.
var arr = [];
arr.push(quote.quoteSummary.result[0].Profile.sector); // Technology
arr.push(quote.quoteSummary.result[0].Owners[1].name); // Susan
arr.push(quote.quoteSummary.result[0].Profit); // 100
但由于有许多特定属性需要读取,因此我想使用循环:
var quote = JSON.parse(response.getContentText());
var arr = [];
var el = [
['Profile', 'sector'],
['Owners[1]', 'name'],
['Profit']
];
for (i = 0; i < el.length; i++)
{
if (quote.quoteSummary.result[0][el[i][0]][el[i][1]] !== undefined)
{
arr.push(quote.quoteSummary.result[0][el[i][0]][el[i][1]].value);
}
}
/*
Expected output (if I would loop through arr):
Technology
Susan
100
*/
但这是行不通的。什么是通用循环,它完成了我在这里要做的事情?如果有帮助的话,定义所需内容的数组也可以以不同的方式构造。但关键是,我不会以这样的脚本结束:
arr.push(quote.quoteSummary.result[0].Profile.something);
arr.push(quote.quoteSummary.result[0].Profile.something);
arr.push(quote.quoteSummary.result[0].Profile.something);
arr.push(quote.quoteSummary.result[0].Profile.something);
arr.push(quote.quoteSummary.result[0].Profile.something);
arr.push(quote.quoteSummary.result[0].Profile.something);
arr.push(quote.quoteSummary.result[0].Profile.something);
arr.push(quote.quoteSummary.result[0].Profile.something);
arr.push(quote.quoteSummary.result[0].Profile.something);
arr.push(quote.quoteSummary.result[0].Profile.something);
arr.push(quote.quoteSummary.result[0].Profile.something);
您应该能够做到这一点:
if(y.value){
arr.push(y.value);
} else {
arr.push(y);
}
但是,如果y.value恰好是0、false等值,则会中断。
如果是这种情况,您可以执行以下操作:
if(y.hasOwnProperty("value")){
arr.push(y.value);
} else {
arr.push(y);
}
根据评论:
if (quote.quoteSummary.result[0][el[i][0]].hasOwnProperty("value")) {
arr.push(quote.quoteSummary.result[0][el[i][0]].value);
} else {
arr.push(quote.quoteSummary.result[0][el[i][0]]);
}
我建议您使用可变长度的属性名称链。给定链中的每个名称都代表一个更深层次的属性。您可以使用以下代码通过任意数量的属性名“潜入”对象:
let dive = (obj, propertyNames) => {
for (let pn of propertyNames) obj = obj[pn];
return obj;
};
现在你可以说:
let dive=(对象,属性名称)=>{
对于(不动产名称的pn)obj=obj[pn];
返回obj;
};
让我们引用={
引文摘要:{
结果:[
{
简介:{
部门:"科技",,
网站:“www.test.com”
},
业主:[
{
姓名:'埃里克',
年龄:'28'
},
{
姓名:'苏珊',
年龄:'44'
}
],
利润:100,
资产:7000
}
]
}
};
//以下是“可变长度属性链”:
设el=[
[“配置文件”、“扇区”],
[“所有者”,1,“名称”],
[“利润”]
];
//下面是如何组合'el'、'dive'和'quote'数据以获得结果:
设arr=el.map(propertyNames=>dive(quote.quoteSummary.result[0],propertyNames));
控制台日志(arr)代码>我不清楚目标到底是什么。你们有输入和输出数据的例子吗?这对你有用吗<代码>var value=typeof y=='object'&&y.hasOwnProperty(值)?y、 value:y
目标确实是检查y1 y2 y3
是否有名为value
的属性,或者如果y1 y2 y3
是值本身,如果我解释得不好,请道歉。在我的情况下,您的解决方案将是:if(fmt=typeof quote.quoteSummary.result[0][el[i][0]][el[i][1]]&"e.quoteSummary.result[0][el[i][1]]和"e.quoteSummary.result[0][el[i][1]]hasOwnProperty(“value”){arr.push(quote.quote.quoteSummary.result[0][el[i][1].value];}
但这会引发一个错误“引用未定义”?谢谢您的回复。我尝试了if(quote.quoteSummary.result[0][el[I][0]].hasOwnProperty(“value”){arr.push(quote.quoteSummary.result[0][el[I][0]]][el[I][1].value];}
但是我得到了一个错误“引用不存在”?我认为应该是这样的:if(quote.quoteSummary.result[0][el[I][0].hasOwnProperty(“value”){arr.push(quote.quoteSummary.result[0][el[i][0]].value);}else{arr.push(quote.quoteSummary.result[0][el[i][0]];}
if (quote.quoteSummary.result[0][el[i][0]].hasOwnProperty("value")) {
arr.push(quote.quoteSummary.result[0][el[i][0]].value);
} else {
arr.push(quote.quoteSummary.result[0][el[i][0]]);
}
let dive = (obj, propertyNames) => {
for (let pn of propertyNames) obj = obj[pn];
return obj;
};