我是一名React开发人员,在Vanilla JS中需要完成一些任务,我不知道我在这里做错了什么。
这是一个简单的选择
<select id="js-year-selected" onchange="getYear()" class="js-year-input">
<option value="2020">2020</option>
<option value="2019" >2019</option>
<option value="2018">2018</option>
<option value="2017">2017</option>
<option value="2016" >2016</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
</select>
这是onchange功能:-
function getYear() {
var getYear = document.getElementById("js-year-selected");
var strUser = getYear.options[getYear.selectedIndex].value;
var getJSON = document.getElementById("json-input").innerHTML;
data = JSON.parse(getJSON);
var year = strUser;
console.log(year);
let filterDataUtil = data.filter(function(dataItem) {
return dataItem.birthday.split("/")[2].localeCompare(strUser) !== -1;
});
console.log(filterDataUtil)
}
无论我说的是哪一年,它只返回2020年的值并将其附加新值。
示例JSON
<textarea class="app__txt js-json" id="json-input" placeholder="Paste the json here.">[
{
"name": "Tyrion Lannister",
"birthday": "12/02/2020"
}, {
"name": "Cersei Lannister",
"birthday": "11/30/1975"
}, {
"name": "Daenerys Targaryen",
"birthday": "11/24/1991"
}, {
"name": "Arya Stark",
"birthday": "11/25/2020"
}, {
"name": "Jon Snow",
"birthday": "12/03/1989"
}, {
"name": "Sansa Stark",
"birthday": "08/15/1992"
}, {
"name": "Jorah Mormont",
"birthday": "12/16/1968"
}
]</textarea>
答案 0 :(得分:2)
您使用的--use derived table and case statment:
--you can give your date in case statement and give proper aliases in order to avoid errors during execution
--this query will give you all records from info table and you can also get info for availability and not available also
SELECT *
FROM (
SELECT Availability.availability_id, Availability.availability_date, Availability.availability_start, Availability.availability_end, Info.info_id, Info.info_firstname, Info.info_surname, Job.job_name
,case when Availability.availability_date = '2019-09-20' then 'AVAILABLE'
ELSE 'NOT AVAILABLE'
END STATUS
FROM Info
left join Availability
ON Availability.availability_info_id = Info.info_id
LEFT JOIN Job
ON Job.job_id = Info.info_job_id
WHERE Availability.availability_date = '2019-09-20'; --//today
) T
WHERE STATUS='NOT AVAILABLE' --THIS WILL GIVE YOU INFO WHICH HAS NO '2019-09-20' IN availability_date COLUMN
错误-当字符串相等时,它返回localeCompare
,或者根据两个字符串的比较方式,返回负/正数。
在这种情况下,您只有两个包含年份的字符串,因此只需使用0
进行比较。
答案 1 :(得分:2)
需要简单的比较
dataItem.birthday.split("/")[2] === strUser;
function getYear() {
var getYear = document.getElementById("js-year-selected");
var strUser = getYear.options[getYear.selectedIndex].value;
var getJSON = document.getElementById("json-input").innerHTML;
data = JSON.parse(getJSON);
var year = strUser;
let filterDataUtil = data.filter(function(dataItem) {
return dataItem.birthday.split("/")[2] === strUser;
});
}
<select id="js-year-selected" onchange="getYear()" class="js-year-input">
<option value="2020">2020</option>
<option value="2019" >2019</option>
<option value="2018">2018</option>
<option value="2017">2017</option>
<option value="2016" >2016</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
</select>
<textarea class="app__txt js-json" id="json-input" placeholder="Paste the json here.">[
{
"name": "Tyrion Lannister",
"birthday": "12/02/2020"
}, {
"name": "Cersei Lannister",
"birthday": "11/30/1975"
}, {
"name": "Daenerys Targaryen",
"birthday": "11/24/1991"
}, {
"name": "Arya Stark",
"birthday": "11/25/2020"
}, {
"name": "Jon Snow",
"birthday": "12/03/1989"
}, {
"name": "Sansa Stark",
"birthday": "08/15/1992"
}, {
"name": "Jorah Mormont",
"birthday": "12/16/1968"
}
]</textarea>
答案 2 :(得分:1)
您需要比较两个字符串:
function getYear() {
var getYear = document.getElementById("js-year-selected");
var strUser = getYear.options[getYear.selectedIndex].value;
var getJSON = document.getElementById("json-input").innerHTML;
data = JSON.parse(getJSON);
var year = strUser;
console.log(year);
let filterDataUtil = data.filter(function(dataItem) {
if(dataItem.birthday.split("/")[2] === strUser){
return dataItem;
};
});
console.log(filterDataUtil)
}
答案 3 :(得分:0)
如果要获取Array,则可以按照以下说明进行操作
let filterDataUtil = data.filter(function(dataItem) {
return dataItem.birthday.split("/")[2] == strUser;
});
console.log(filterDataUtil);
如果只想获取对象,则可以按照以下说明进行操作
let filterDataUtil = data.find(dataItem => dataItem.birthday.split("/")[2] == strUser);
console.log(filterDataUtil);