I have a json response, where in 'products' I have 'funds' and inside funds I have a date field. i want to view the date in template.
I have used forEach for iterating the products and then iterated the funds, then assigned the date to a variable. This is done inside the computed().
Now i want the date to be fetched in the template. But I am not able to fetch it.
I have tried adding the same in methods(), but no success
data: function(){
return {
date: ''
}
},
computed: {
products: function(){
let products = this.sortedSchemeData.products;
var year = this.selectedSchemeYear;
products.forEach(function(product){
product.funds.forEach(function(fund){
var date = fund.fundsProcessDate;
console.log(date); //some date from json
})
});
return products;
}
},
<template>
<div>
<p>date: <b>{{date}}</b></p>
</template>
date: date in json
The products[0].funds[0].fundProcessDate
doesn't handle the case when the first product
is empty. My example below does handle it.
computed: {
date() {
const found = this.products.find(product =>
product.funds.some(fund => fund.fundsProcessDate != null)
);
return (found && found.funds[0].fundsProcessDate) || "01-01-2019"; //default date if not found from all of them
}
}
You can use Computed-Setter for this scenario.
data: function(){
return {
date: ''
}
},
computed: {
products: {
// setter
set: function (newValue) {
let products = newValue;
var year = this.selectedSchemeYear;
products.forEach(function(product){
product.funds.forEach(function(fund){
this.date = fund.fundsProcessDate;
})
});
}
}
},
<template>
<div>
<p>date: <b>{{date}}</b></p>
</template>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.