我的数据结构如下:
[
{title: 'a', washday: 'true', cutday: 'false', colorday: 'true', deepconditionday: 'true'},
{title: 'b', washday: 'false', cutday: 'true', colorday: 'false', deepconditionday: 'false'},
{title: 'c', washday: 'true', cutday: 'false', colorday: 'true', deepconditionday: 'true'},
{title: 'd', washday: 'false', cutday: 'false', colorday: 'false', deepconditionday: 'false'},
]
我的应用程序中还有 cutday , colorday , deepconditionday 和 washday 的状态,一个在布尔值true和false之间切换的开关。
如何根据打开的状态过滤上述数据?例如,如果仅启用colorday,则我要显示colorday为“ true”的所有项目。如果将deepconditionday和cutday都打开,那么我要显示出cutday和deepconditionday均为“ true”的所有项目。
谢谢。
答案 0 :(得分:0)
可能是这样的:
let titleFilter = "a";
let washdayFilter = true;
let cutdayFilter = true;
let filteredData = data.filter(
(datapoint) =>
datapoint.title == titleFilter &&
datapoint.washday == washdayFilter.toString() &&
datapoint.cutday == cutdayFilter.toString()
);
答案 1 :(得分:0)
static func registerSubscriptionWith(ID: String) {
let predicate = NSPredicate(format: "channelID == %@", ID)
let subscription = CKQuerySubscription(recordType: "Episode", predicate: predicate, options: .firesOnRecordCreation)
let myContainer = CKContainer.default()
let publicDatabase = myContainer.publicCloudDatabase
let recordID = CKRecord.ID(recordName: ID)
publicDatabase.fetch(withRecordID: recordID) { (record, error) in
if let error = error {
print("Error: \(error.localizedDescription)")
} else {
if record != nil {
let notification = CKSubscription.NotificationInfo()
notification.title = record!["channelName"]
notification.alertBody = record!["caption"]
notification.soundName = "default"
subscription.notificationInfo = notification
publicDatabase.save(subscription) { result, error in
if error != nil {
print(error!.localizedDescription)
} else {
print("Success subscribing user to episode notifications")
}
}
}
}
}
}
更多信息在这里: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
请注意,您使用'true'和'false'作为字符串,而不是布尔值,因此我也这样做。 您应该使用布尔值并相应地更改代码 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
答案 2 :(得分:0)
我已经实现了使用ReactJS和MaterialUI的示例。只需将状态用于开关并按状态过滤即可。链接演示:https://codesandbox.io/s/material-demo-forked-176e3?file=/demo.js
awk 'match($0,/{[^\\]*/){print substr($0,RSTART+1,RLENGTH-1)}' Input_file