嗨,我在遍历javascript对象时遇到困难。如何从两者中获取scheme_name和NAV并将其存储在变量中,例如“您有2个与帐户关联的方案。scheme_name1 NAV值为“” scheme_name2 NAV值为“”,依此类推。请向我解释一下thanx
let data = [{
"CustomerID": 12345,
"NAV": "95.24059718",
"cost_of_purchase": 799900,
"folio_number": 10007060,
"id": 1,
"mutual_fund_house": "AXIS MUTUAL FUND",
"no_of_units": 15000,
"option": "GROWTH",
"plan": "REGULAR",
"resource_uri": "/api/v1/folio/1/",
"scheme_name": "AXIS LONG TERM EQUITY",
"value_of_units": "1428608.9580"
}, {
"CustomerID": 12345,
"NAV": "1053.31517400",
"cost_of_purchase": 1500000,
"folio_number": 5540000567,
"id": 2,
"mutual_fund_house": "SBI Mutual Fund",
"no_of_units": 2750,
"option": "DIVIDEND",
"plan": "DIRECT",
"resource_uri": "/api/v1/folio/2/",
"scheme_name": "SBI Magnum Multicap Fund",
"value_of_units": "2896616.7270"
}]
答案 0 :(得分:0)
您有一个data
数组,因此首先需要遍历该数组。
选择每个记录要保留的属性,并将其保存到results
数组中。
一旦获得结果,就可以遍历结果并打印出个人记录。
let data = [{"CustomerID":12345,"NAV":"95.24059718","cost_of_purchase":799900,"folio_number":10007060,"id":1,"mutual_fund_house":"AXIS MUTUAL FUND","no_of_units":15000,"option":"GROWTH","plan":"REGULAR","resource_uri":"/api/v1/folio/1/","scheme_name":"AXIS LONG TERM EQUITY","value_of_units":"1428608.9580"},{"CustomerID":12345,"NAV":"1053.31517400","cost_of_purchase":1500000,"folio_number":5540000567,"id":2,"mutual_fund_house":"SBI Mutual Fund","no_of_units":2750,"option":"DIVIDEND","plan":"DIRECT","resource_uri":"/api/v1/folio/2/","scheme_name":"SBI Magnum Multicap Fund","value_of_units":"2896616.7270"}]
let results = []
data.forEach(datum => {
results.push({
scheme_name: datum.scheme_name,
nav: datum.NAV,
})
})
console.log(`You've got ${results.length} items in your account.`)
results.forEach(result => {
console.log(`${result.scheme_name} - NAV value is: ${result.nav}`)
})
答案 1 :(得分:0)
想象一下您的每个对象(在本例中,您提供了两(2)个)作为数组内部的关联数组。意味着您可以通过键来引用它,而不是通过它的索引位置来引用它。
这意味着要访问对象1,您将必须写入数据[0]。但是,如果您对此发出警告,则只会告诉您data [0]是一个对象。是。
要访问该数组中的实际值,则必须提供键,您可以通过提供数字来完成,或者在您的情况下,更容易的是,它是关联的键,即“ scheme_name”。
请参阅以下内容:
let data = [{
"CustomerID": 12345,
"NAV": "95.24059718",
"cost_of_purchase": 799900,
"folio_number": 10007060,
"id": 1,
"mutual_fund_house": "AXIS MUTUAL FUND",
"no_of_units": 15000,
"option": "GROWTH",
"plan": "REGULAR",
"resource_uri": "/api/v1/folio/1/",
"scheme_name": "AXIS LONG TERM EQUITY",
"value_of_units": "1428608.9580"
}, {
"CustomerID": 12345,
"NAV": "1053.31517400",
"cost_of_purchase": 1500000,
"folio_number": 5540000567,
"id": 2,
"mutual_fund_house": "SBI Mutual Fund",
"no_of_units": 2750,
"option": "DIVIDEND",
"plan": "DIRECT",
"resource_uri": "/api/v1/folio/2/",
"scheme_name": "SBI Magnum Multicap Fund",
"value_of_units": "2896616.7270"
}]
for (let i = 0; i < data.length; i++) {
alert(data[i]["scheme_name"]);
}
非常简单,对于i = 0,且i小于您名为data的数组中的关联数组的数量,请通过关联的键来提醒该索引数组上的值。
答案 2 :(得分:0)
您似乎试图将其映射到另一个对象。
首先,尝试阅读和理解数组方法,您可以检查:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
对于地图方法,您可以检查:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
作为具体答案,您可以执行以下操作:
const mappedData = data.map(function(item) {
return {
scheme_name: item.scheme_name,
NAV: item.NAV
};
});
这将为您返回简化版本的数组。
之后,您可以将其转换为类似以下的字符串:
let solution = `You have ${mappedData.length} schemes linked to your account. `;
mappedData.forEach(function(mapped) {
solution += mapped.scheme_name + ' is ' + mapped.NAV + ' '
});
注意:我展示了map方法,用于深入了解您可以做什么,通常不需要简化,可以直接用于每个版本。
答案 3 :(得分:-1)
我为您创建了示例fiddle。您需要遍历主对象中的每个对象,并将所有信息存储在外部。
data.forEach(function(item) {
console.log(item.scheme_name);
});