我对编码很陌生,目前我正在编写一个React原生项目。对不起,如果问题太简单了。我已经尝试了很多方法来导航但失败了。 我有一个JSON文件,数据如下。
{
"values" : {
"EquityTechnology": {
"_figure": [
9.138790534659202
],
"ICICIPrudentialTechnologyFundGrowth": {
"_figure": [
10.6484932862722
]
},
"Franklin Infotech Fund-Growth": {
"_figure": [
7.96902834459266
]
},
"Birla Sun Life New Millenium Fund - Growth - Regular Plan": {
"_figure": [
8.79884997311275
]
},
"SBI IT FUND - REGULAR PLAN - GROWTH": {
"_figure": [
null
]
}
},
"Equity - SmallCap": {
"_figure": [
14.0064060591969
],
"HDFC Small Cap Fund - Regular Growth Plan": {
"_figure": [
null
]
},
"Reliance Mid & Small Cap Fund -Growth Plan -Growth Option": {
"_figure": [
11.8481966627509
]
},
"Franklin India Smaller Companies Fund-Growth": {
"_figure": [
15.3302065566009
]
},
"Sundaram S.M.I.L.E.Fund-Growth": {
"_figure": [
11.754220292209
]
},
"SBI Magnum MIDCAP FUND - REGULAR PLAN - GROWTH": {
"_figure": [
14.9351631127958
]
},
"Birla Sun Life Small & Midcap Fund - GROWTH": {
"_figure": [
12.3666676648488
]
},
"L&T Emerging Businesses Fund - Regular Plan - Growth Option": {
"_figure": [
null
]
},
"Canara Robeco Emerging Equities - Regular Plan - GROWTH": {
"_figure": [
14.6579568171628
]
},
"Reliance Small Cap Fund - Growth Plan - Growth Option": {
"_figure": [
15.0019513606657
]
},
"DSP BlackRock Micro Cap Fund - Regular - Growth": {
"_figure": [
16.1568860065413
]
}
},
"Equity - Pharma": {
"_figure": [
12.966782129169834
],
"SBI PHARMA - REGULAR PLAN -GROWTH": {
"_figure": [
14.9918670876748
]
},
"UTI Pharma & Healthcare Fund-Growth Option": {
"_figure": [
11.2119667676491
]
},
"Reliance Pharma Fund-Growth Plan-Growth Option": {
"_figure": [
12.6965125321856
]
}
},
"Equity - MultiCap": {
"DSP BlackRock Equity Fund - Regular Plan - Growth": {
"_figure": [
8.32832307169916
]
},
"DSP BlackRock Opportunities Fund-Regular Plan - Growth": {
"_figure": [
9.72369143832476
]
},
"MOSt Focused Multicap 35-Regular Plan-Growth Option": {
"_figure": [
null
]
},
"Reliance Equity Opportunities Fund-Growth Plan-Growth Option": {
"_figure": [
9.79339882843478
]
},
"L&T Equity Fund-Regular Plan-Growth Option": {
"_figure": [
null
]
},
"IIFL India Growth Fund -Regular Plan - Growth": {
"_figure": [
null
]
},
"SBI Magnum Multicap Fund - REGULAR PLAN -Growth Option": {
"_figure": [
10.1704693596904
]
},
"Kotak Select Focus Fund - Growth": {
"_figure": [
10.7785458258239
]
},
"HDFC Equity Fund - Growth Option": {
"_figure": [
8.29168295966879
]
},
"Franklin India Flexi Cap Fund-Growth Plan": {
"_figure": [
9.52777086776355
]
},
"_figure": [
9.998290130663644
],
"Franklin India High Growth Companies Fund - Growth Plan": {
"_figure": [
12.0111958711599
]
},
"Birla Sun Life Equity Fund - Growth - Regular Plan": {
"_figure": [
10.5597033707683
]
},
"ICICI Prudential Value Discovery Fund - Growth": {
"_figure": [
12.494420125849
]
},
"Axis Equity Fund - Growth": {
"_figure": [
8.30198971811754
]
}
},
"Equity - FMCG": {
"_figure": [
12.9391809175777
],
"SBI FMCG FUND - REGULAR - GROWTH": {
"_figure": [
null
]
},
"ICICI Prudential FMCG Fund - Growth": {
"_figure": [
12.9391809175777
]
}
},
"Equity - Banking": {
"_figure": [
7.756301964533782
],
"UTI Banking Sector Fund-Income Option": {
"_figure": [
4.48544488820235
]
},
"Kotak Banking ETF": {
"_figure": [
null
]
},
"Birla Sun Life Banking and Financial Services Fund - Regular Plan - Growth": {
"_figure": [
null
]
},
"R*Shares Bank BeES": {
"_figure": [
null
]
},
"Reliance Banking Fund-Growth Plan-Growth Option": {
"_figure": [
8.2686147173821
]
},
"SBI BANKING & FINANCIAL SERVICES FUND - REGULAR PLAN - GROWTH": {
"_figure": [
null
]
},
"ICICI Prudential Banking and Financial Services Fund - Growth": {
"_figure": [
11.0147287026105
]
},
"UTI Banking Sector Fund-Growth Option": {
"_figure": [
7.25641954994018
]
}
},
"Gold Fund": {
"R*Shares Gold BeES": {
"_figure": [
null
]
},
"_figure": [
4.0762242905284065
],
"HDFC Gold Exchange Traded Fund": {
"_figure": [
6.03459415600124
]
},
"SBI GOLD EXCHANGE TRADED SCHEME": {
"_figure": [
6.14783904734468
]
},
"KOTAK GOLD ETF": {
"_figure": [
-3.40119263557552
]
},
"Kotak Gold Fund Growth": {
"_figure": [
5.61706623223513
]
},
"Axis Gold ETF": {
"_figure": [
5.9828146526365
]
}
}
}
}
我想使用索引浏览JSON,因为数据将是动态的,名称可能会随着时间而变化。我该怎么做才能访问,例如,' 14.9918670876748'在' _figure' ' SBI PHARMA - 常规计划-GROWTH'而反过来是公平 - 制药'。我想找回' _figure'所有项目。我试过for..in循环,但它只返回嵌套对象的索引。有什么办法可以使用索引浏览JSON文件吗?
提前谢谢。
抱歉英语不好。
答案 0 :(得分:1)
var rObj = {
"values" : {
"EquityTechnology": {
"_figure": [
9.138790534659202
],
"ICICIPrudentialTechnologyFundGrowth": {
"_figure": [
10.6484932862722
]
},
"Franklin Infotech Fund-Growth": {
"_figure": [
7.96902834459266
]
},
"Birla Sun Life New Millenium Fund - Growth - Regular Plan": {
"_figure": [
8.79884997311275
]
},
"SBI IT FUND - REGULAR PLAN - GROWTH": {
"_figure": [
null
]
}
},
"Equity - SmallCap": {
"_figure": [
14.0064060591969
],
"HDFC Small Cap Fund - Regular Growth Plan": {
"_figure": [
null
]
},
"Reliance Mid & Small Cap Fund -Growth Plan -Growth Option": {
"_figure": [
11.8481966627509
]
},
"Franklin India Smaller Companies Fund-Growth": {
"_figure": [
15.3302065566009
]
},
"Sundaram S.M.I.L.E.Fund-Growth": {
"_figure": [
11.754220292209
]
},
"SBI Magnum MIDCAP FUND - REGULAR PLAN - GROWTH": {
"_figure": [
14.9351631127958
]
},
"Birla Sun Life Small & Midcap Fund - GROWTH": {
"_figure": [
12.3666676648488
]
},
"L&T Emerging Businesses Fund - Regular Plan - Growth Option": {
"_figure": [
null
]
},
"Canara Robeco Emerging Equities - Regular Plan - GROWTH": {
"_figure": [
14.6579568171628
]
},
"Reliance Small Cap Fund - Growth Plan - Growth Option": {
"_figure": [
15.0019513606657
]
},
"DSP BlackRock Micro Cap Fund - Regular - Growth": {
"_figure": [
16.1568860065413
]
}
},
"Equity - Pharma": {
"_figure": [
12.966782129169834
],
"SBI PHARMA - REGULAR PLAN -GROWTH": {
"_figure": [
14.9918670876748
]
},
"UTI Pharma & Healthcare Fund-Growth Option": {
"_figure": [
11.2119667676491
]
},
"Reliance Pharma Fund-Growth Plan-Growth Option": {
"_figure": [
12.6965125321856
]
}
},
"Equity - MultiCap": {
"DSP BlackRock Equity Fund - Regular Plan - Growth": {
"_figure": [
8.32832307169916
]
},
"DSP BlackRock Opportunities Fund-Regular Plan - Growth": {
"_figure": [
9.72369143832476
]
},
"MOSt Focused Multicap 35-Regular Plan-Growth Option": {
"_figure": [
null
]
},
"Reliance Equity Opportunities Fund-Growth Plan-Growth Option": {
"_figure": [
9.79339882843478
]
},
"L&T Equity Fund-Regular Plan-Growth Option": {
"_figure": [
null
]
},
"IIFL India Growth Fund -Regular Plan - Growth": {
"_figure": [
null
]
},
"SBI Magnum Multicap Fund - REGULAR PLAN -Growth Option": {
"_figure": [
10.1704693596904
]
},
"Kotak Select Focus Fund - Growth": {
"_figure": [
10.7785458258239
]
},
"HDFC Equity Fund - Growth Option": {
"_figure": [
8.29168295966879
]
},
"Franklin India Flexi Cap Fund-Growth Plan": {
"_figure": [
9.52777086776355
]
},
"_figure": [
9.998290130663644
],
"Franklin India High Growth Companies Fund - Growth Plan": {
"_figure": [
12.0111958711599
]
},
"Birla Sun Life Equity Fund - Growth - Regular Plan": {
"_figure": [
10.5597033707683
]
},
"ICICI Prudential Value Discovery Fund - Growth": {
"_figure": [
12.494420125849
]
},
"Axis Equity Fund - Growth": {
"_figure": [
8.30198971811754
]
}
},
"Equity - FMCG": {
"_figure": [
12.9391809175777
],
"SBI FMCG FUND - REGULAR - GROWTH": {
"_figure": [
null
]
},
"ICICI Prudential FMCG Fund - Growth": {
"_figure": [
12.9391809175777
]
}
},
"Equity - Banking": {
"_figure": [
7.756301964533782
],
"UTI Banking Sector Fund-Income Option": {
"_figure": [
4.48544488820235
]
},
"Kotak Banking ETF": {
"_figure": [
null
]
},
"Birla Sun Life Banking and Financial Services Fund - Regular Plan - Growth": {
"_figure": [
null
]
},
"R*Shares Bank BeES": {
"_figure": [
null
]
},
"Reliance Banking Fund-Growth Plan-Growth Option": {
"_figure": [
8.2686147173821
]
},
"SBI BANKING & FINANCIAL SERVICES FUND - REGULAR PLAN - GROWTH": {
"_figure": [
null
]
},
"ICICI Prudential Banking and Financial Services Fund - Growth": {
"_figure": [
11.0147287026105
]
},
"UTI Banking Sector Fund-Growth Option": {
"_figure": [
7.25641954994018
]
}
},
"Gold Fund": {
"R*Shares Gold BeES": {
"_figure": [
null
]
},
"_figure": [
4.0762242905284065
],
"HDFC Gold Exchange Traded Fund": {
"_figure": [
6.03459415600124
]
},
"SBI GOLD EXCHANGE TRADED SCHEME": {
"_figure": [
6.14783904734468
]
},
"KOTAK GOLD ETF": {
"_figure": [
-3.40119263557552
]
},
"Kotak Gold Fund Growth": {
"_figure": [
5.61706623223513
]
},
"Axis Gold ETF": {
"_figure": [
5.9828146526365
]
}
}
}
}
//console.log(rObj.values;);
//container
var values = rObj.values;
//Index
var find = "Equity - Pharma";
//subIndex
var subFind = "SBI PHARMA - REGULAR PLAN -GROWTH";
var case1 = values[find][subFind]["_figure"][0];
If you want to find the specific just change the variables the corresponding
names [find] i.e "Equity - Pharma" then subFind i.e "SBI PHARMA - REGULAR
PLAN -GROWTH"
for(var index in values) {
if(index === find) {
var nextIndex = values[index];
for(var nextFind in nextIndex) {
var Item = nextIndex[nextFind];
if(nextFind === subFind) {
console.log(Item["_figure"][0]);
}
}
}
}
答案 1 :(得分:0)
Object.prototype.hasOwnProperty()
应该做到这一点。这将在迭代时为您提供关键和值。
例如:
var buz = {
fog: 'stack'
};
for (var name in buz) {
if (buz.hasOwnProperty(name)) {
console.log('this is fog (' +
name + ') for sure. Value: ' + buz[name]);
}
else {
console.log(name); // toString or something else
}
}
答案 2 :(得分:0)
使用它从对象中提取所有_figure作为数组。
var figures=[];
Object.keys(data.values).forEach(function(val){
Object.keys(data.values[val]).forEach(function(_val){
if(data.values[val][_val]&&data.values[val][_val]._figure){
figures.push(data.values[val][_val]._figure[0]);
}
});
})
console.log(figures);
<强> SNIPPET-1 强>
var data = {
"values" : {
"EquityTechnology": {
"_figure": [
9.138790534659202
],
"ICICIPrudentialTechnologyFundGrowth": {
"_figure": [
10.6484932862722
]
},
"Franklin Infotech Fund-Growth": {
"_figure": [
7.96902834459266
]
},
"Birla Sun Life New Millenium Fund - Growth - Regular Plan": {
"_figure": [
8.79884997311275
]
},
"SBI IT FUND - REGULAR PLAN - GROWTH": {
"_figure": [
null
]
}
},
"Equity - SmallCap": {
"_figure": [
14.0064060591969
],
"HDFC Small Cap Fund - Regular Growth Plan": {
"_figure": [
null
]
},
"Reliance Mid & Small Cap Fund -Growth Plan -Growth Option": {
"_figure": [
11.8481966627509
]
},
"Franklin India Smaller Companies Fund-Growth": {
"_figure": [
15.3302065566009
]
},
"Sundaram S.M.I.L.E.Fund-Growth": {
"_figure": [
11.754220292209
]
},
"SBI Magnum MIDCAP FUND - REGULAR PLAN - GROWTH": {
"_figure": [
14.9351631127958
]
},
"Birla Sun Life Small & Midcap Fund - GROWTH": {
"_figure": [
12.3666676648488
]
},
"L&T Emerging Businesses Fund - Regular Plan - Growth Option": {
"_figure": [
null
]
},
"Canara Robeco Emerging Equities - Regular Plan - GROWTH": {
"_figure": [
14.6579568171628
]
},
"Reliance Small Cap Fund - Growth Plan - Growth Option": {
"_figure": [
15.0019513606657
]
},
"DSP BlackRock Micro Cap Fund - Regular - Growth": {
"_figure": [
16.1568860065413
]
}
},
"Equity - Pharma": {
"_figure": [
12.966782129169834
],
"SBI PHARMA - REGULAR PLAN -GROWTH": {
"_figure": [
14.9918670876748
]
},
"UTI Pharma & Healthcare Fund-Growth Option": {
"_figure": [
11.2119667676491
]
},
"Reliance Pharma Fund-Growth Plan-Growth Option": {
"_figure": [
12.6965125321856
]
}
},
"Equity - MultiCap": {
"DSP BlackRock Equity Fund - Regular Plan - Growth": {
"_figure": [
8.32832307169916
]
},
"DSP BlackRock Opportunities Fund-Regular Plan - Growth": {
"_figure": [
9.72369143832476
]
},
"MOSt Focused Multicap 35-Regular Plan-Growth Option": {
"_figure": [
null
]
},
"Reliance Equity Opportunities Fund-Growth Plan-Growth Option": {
"_figure": [
9.79339882843478
]
},
"L&T Equity Fund-Regular Plan-Growth Option": {
"_figure": [
null
]
},
"IIFL India Growth Fund -Regular Plan - Growth": {
"_figure": [
null
]
},
"SBI Magnum Multicap Fund - REGULAR PLAN -Growth Option": {
"_figure": [
10.1704693596904
]
},
"Kotak Select Focus Fund - Growth": {
"_figure": [
10.7785458258239
]
},
"HDFC Equity Fund - Growth Option": {
"_figure": [
8.29168295966879
]
},
"Franklin India Flexi Cap Fund-Growth Plan": {
"_figure": [
9.52777086776355
]
},
"_figure": [
9.998290130663644
],
"Franklin India High Growth Companies Fund - Growth Plan": {
"_figure": [
12.0111958711599
]
},
"Birla Sun Life Equity Fund - Growth - Regular Plan": {
"_figure": [
10.5597033707683
]
},
"ICICI Prudential Value Discovery Fund - Growth": {
"_figure": [
12.494420125849
]
},
"Axis Equity Fund - Growth": {
"_figure": [
8.30198971811754
]
}
},
"Equity - FMCG": {
"_figure": [
12.9391809175777
],
"SBI FMCG FUND - REGULAR - GROWTH": {
"_figure": [
null
]
},
"ICICI Prudential FMCG Fund - Growth": {
"_figure": [
12.9391809175777
]
}
},
"Equity - Banking": {
"_figure": [
7.756301964533782
],
"UTI Banking Sector Fund-Income Option": {
"_figure": [
4.48544488820235
]
},
"Kotak Banking ETF": {
"_figure": [
null
]
},
"Birla Sun Life Banking and Financial Services Fund - Regular Plan - Growth": {
"_figure": [
null
]
},
"R*Shares Bank BeES": {
"_figure": [
null
]
},
"Reliance Banking Fund-Growth Plan-Growth Option": {
"_figure": [
8.2686147173821
]
},
"SBI BANKING & FINANCIAL SERVICES FUND - REGULAR PLAN - GROWTH": {
"_figure": [
null
]
},
"ICICI Prudential Banking and Financial Services Fund - Growth": {
"_figure": [
11.0147287026105
]
},
"UTI Banking Sector Fund-Growth Option": {
"_figure": [
7.25641954994018
]
}
},
"Gold Fund": {
"R*Shares Gold BeES": {
"_figure": [
null
]
},
"_figure": [
4.0762242905284065
],
"HDFC Gold Exchange Traded Fund": {
"_figure": [
6.03459415600124
]
},
"SBI GOLD EXCHANGE TRADED SCHEME": {
"_figure": [
6.14783904734468
]
},
"KOTAK GOLD ETF": {
"_figure": [
-3.40119263557552
]
},
"Kotak Gold Fund Growth": {
"_figure": [
5.61706623223513
]
},
"Axis Gold ETF": {
"_figure": [
5.9828146526365
]
}
}
}
};
var figures=[];
Object.keys(data.values).forEach(function(val){
Object.keys(data.values[val]).forEach(function(_val){
if(data.values[val][_val]&&data.values[val][_val]._figure){
figures.push(data.values[val][_val]._figure[0]);
}
});
})
console.log(figures);
&#13;
或者你可以试试这个。
JSON.stringify(data).match(/"_figure":\[[0-9.]+\]/g).map(function(val){
return parseFloat(val.match(/[0-9.]+/g)[0])
});
<强> SNIPPET-2 强>
var data = {
"values" : {
"EquityTechnology": {
"_figure": [
9.138790534659202
],
"ICICIPrudentialTechnologyFundGrowth": {
"_figure": [
10.6484932862722
]
},
"Franklin Infotech Fund-Growth": {
"_figure": [
7.96902834459266
]
},
"Birla Sun Life New Millenium Fund - Growth - Regular Plan": {
"_figure": [
8.79884997311275
]
},
"SBI IT FUND - REGULAR PLAN - GROWTH": {
"_figure": [
null
]
}
},
"Equity - SmallCap": {
"_figure": [
14.0064060591969
],
"HDFC Small Cap Fund - Regular Growth Plan": {
"_figure": [
null
]
},
"Reliance Mid & Small Cap Fund -Growth Plan -Growth Option": {
"_figure": [
11.8481966627509
]
},
"Franklin India Smaller Companies Fund-Growth": {
"_figure": [
15.3302065566009
]
},
"Sundaram S.M.I.L.E.Fund-Growth": {
"_figure": [
11.754220292209
]
},
"SBI Magnum MIDCAP FUND - REGULAR PLAN - GROWTH": {
"_figure": [
14.9351631127958
]
},
"Birla Sun Life Small & Midcap Fund - GROWTH": {
"_figure": [
12.3666676648488
]
},
"L&T Emerging Businesses Fund - Regular Plan - Growth Option": {
"_figure": [
null
]
},
"Canara Robeco Emerging Equities - Regular Plan - GROWTH": {
"_figure": [
14.6579568171628
]
},
"Reliance Small Cap Fund - Growth Plan - Growth Option": {
"_figure": [
15.0019513606657
]
},
"DSP BlackRock Micro Cap Fund - Regular - Growth": {
"_figure": [
16.1568860065413
]
}
},
"Equity - Pharma": {
"_figure": [
12.966782129169834
],
"SBI PHARMA - REGULAR PLAN -GROWTH": {
"_figure": [
14.9918670876748
]
},
"UTI Pharma & Healthcare Fund-Growth Option": {
"_figure": [
11.2119667676491
]
},
"Reliance Pharma Fund-Growth Plan-Growth Option": {
"_figure": [
12.6965125321856
]
}
},
"Equity - MultiCap": {
"DSP BlackRock Equity Fund - Regular Plan - Growth": {
"_figure": [
8.32832307169916
]
},
"DSP BlackRock Opportunities Fund-Regular Plan - Growth": {
"_figure": [
9.72369143832476
]
},
"MOSt Focused Multicap 35-Regular Plan-Growth Option": {
"_figure": [
null
]
},
"Reliance Equity Opportunities Fund-Growth Plan-Growth Option": {
"_figure": [
9.79339882843478
]
},
"L&T Equity Fund-Regular Plan-Growth Option": {
"_figure": [
null
]
},
"IIFL India Growth Fund -Regular Plan - Growth": {
"_figure": [
null
]
},
"SBI Magnum Multicap Fund - REGULAR PLAN -Growth Option": {
"_figure": [
10.1704693596904
]
},
"Kotak Select Focus Fund - Growth": {
"_figure": [
10.7785458258239
]
},
"HDFC Equity Fund - Growth Option": {
"_figure": [
8.29168295966879
]
},
"Franklin India Flexi Cap Fund-Growth Plan": {
"_figure": [
9.52777086776355
]
},
"_figure": [
9.998290130663644
],
"Franklin India High Growth Companies Fund - Growth Plan": {
"_figure": [
12.0111958711599
]
},
"Birla Sun Life Equity Fund - Growth - Regular Plan": {
"_figure": [
10.5597033707683
]
},
"ICICI Prudential Value Discovery Fund - Growth": {
"_figure": [
12.494420125849
]
},
"Axis Equity Fund - Growth": {
"_figure": [
8.30198971811754
]
}
},
"Equity - FMCG": {
"_figure": [
12.9391809175777
],
"SBI FMCG FUND - REGULAR - GROWTH": {
"_figure": [
null
]
},
"ICICI Prudential FMCG Fund - Growth": {
"_figure": [
12.9391809175777
]
}
},
"Equity - Banking": {
"_figure": [
7.756301964533782
],
"UTI Banking Sector Fund-Income Option": {
"_figure": [
4.48544488820235
]
},
"Kotak Banking ETF": {
"_figure": [
null
]
},
"Birla Sun Life Banking and Financial Services Fund - Regular Plan - Growth": {
"_figure": [
null
]
},
"R*Shares Bank BeES": {
"_figure": [
null
]
},
"Reliance Banking Fund-Growth Plan-Growth Option": {
"_figure": [
8.2686147173821
]
},
"SBI BANKING & FINANCIAL SERVICES FUND - REGULAR PLAN - GROWTH": {
"_figure": [
null
]
},
"ICICI Prudential Banking and Financial Services Fund - Growth": {
"_figure": [
11.0147287026105
]
},
"UTI Banking Sector Fund-Growth Option": {
"_figure": [
7.25641954994018
]
}
},
"Gold Fund": {
"R*Shares Gold BeES": {
"_figure": [
null
]
},
"_figure": [
4.0762242905284065
],
"HDFC Gold Exchange Traded Fund": {
"_figure": [
6.03459415600124
]
},
"SBI GOLD EXCHANGE TRADED SCHEME": {
"_figure": [
6.14783904734468
]
},
"KOTAK GOLD ETF": {
"_figure": [
-3.40119263557552
]
},
"Kotak Gold Fund Growth": {
"_figure": [
5.61706623223513
]
},
"Axis Gold ETF": {
"_figure": [
5.9828146526365
]
}
}
}
};
var figures = JSON.stringify(data).match(/"_figure":\[[0-9.]+\]/g).map(function(val){
return parseFloat(val.match(/[0-9.]+/g)[0])
});
console.log(figures);
&#13;