Javascript 显示重复的值
我有一个显示日期的下拉列表:-Javascript 显示重复的值,javascript,angularjs,Javascript,Angularjs,我有一个显示日期的下拉列表:- <label>Date:</label> <select data-ng-model="date" name="viewDate" data-ng-options="d for d in newDates" data-ng-change="selectDate(date)" required="required"> <option value="">Please Select</optio
<label>Date:</label>
<select data-ng-model="date" name="viewDate" data-ng-options="d for d in newDates" data-ng-change="selectDate(date)" required="required">
<option value="">Please Select</option>
</select>
随着日期进入新纪元,我正试图将其转换为人类可读的形式。但每个条目的值都显示为重复。我想我写的转换字符串的代码是错误的。有人能告诉我我的错误吗。如果你不想要重复的值,你可以创建一个集合,而不是一个列表
scope.getDates = function () {
scope.newDates = new Set();
ApiServices.getAllDates().then(
function (response) {
scope.dates = response.data;
scope.dates.forEach(function (entry,index) {
entry = moment(entry, 'YYYY-MM-DD').format();
scope.newDates.add(entry);
});
if (scope.dates.length) {
scope.noRecords = false;
} else {
scope.noRecords = true;
}
},
function (err) {
// Handle error here
console.log('Error' + JSON.stringify(err.data));
});
};
这里是我尝试过的解决方案,它正在工作。现在没有重复的值
scope.getDates = function () {
scope.newDates = new Set();
ApiServices.getAllDates().then(
function (response) {
scope.dates = response.data;
scope.dates.forEach(function (entry,index) {
scope.newDates = scope.dates.map(function(entry){
entry = moment(entry).format('YYYY-MM-DD');
return entry;
});
});
if (scope.dates.length) {
scope.noRecords = false;
} else {
scope.noRecords = true;
}
},
function (err) {
// Handle error here
console.log('Error' + JSON.stringify(err.data));
});
};
您能确认响应中是否没有重复的数据吗?是的,这不是因为当我不将其转换为字符串时,值是唯一的。您可以尝试这个方法,如果它不起作用,我建议您创建一个plunk scope.dates.forEach(function(entry,index){scope.newDates.push(矩.unix(entry.format('YYYY-MM-DD');})上述方法不起作用。“现在没有显示任何值。@shreyagupta我更新了答案,检查这是否对您有效。我认为这不是一个好方法。”。我相信我理解你的问题。您能否共享从服务获得的响应负载,我将添加我的应答响应数据:-[14935104000001494201600001496620800000]我已添加我的应答。你可以查一下。
scope.getDates = function () {
scope.newDates = new Set();
ApiServices.getAllDates().then(
function (response) {
scope.dates = response.data;
scope.dates.forEach(function (entry,index) {
scope.newDates = scope.dates.map(function(entry){
entry = moment(entry).format('YYYY-MM-DD');
return entry;
});
});
if (scope.dates.length) {
scope.noRecords = false;
} else {
scope.noRecords = true;
}
},
function (err) {
// Handle error here
console.log('Error' + JSON.stringify(err.data));
});
};
scope.getDates = function () {
$scope.humanizedDates = [];
ApiServices.getAllDates().then(
function (response) {
if (response.data.length) {
$scope.humanizedDates = response.data.map(function(date){
return moment(date).format('YYYY-MM-DD');
});
scope.noRecords = false;
} else {
scope.noRecords = true;
}
},
function (err) {
// Handle error here
console.log('Error' + JSON.stringify(err.data));
});
};