我有一个看起来像这样的对象:
let data = {
toDate: this.state.toDate,
fromDate: this.state.fromDate,
filteredEntityText: null,
options: "negative",
searchTerm: null
};
Run Code Online (Sandbox Code Playgroud)
从上面的对象中,我想生成一个新对象,该对象仅具有值不为空的键值对。
预期结果应如下所示:
let newData = {
toDate: this.state.toDate,
fromDate: this.state.fromDate,
options: "negative"
};
Run Code Online (Sandbox Code Playgroud)
这里我使用了对象的静态示例data,空值稍后可能会有所不同。基本上我想要一个具有键值对的新对象,其值不是null。谁能帮我解决问题吗?
我有一个自定义端点,如下所示:
add_action( 'rest_api_init', function () {
register_rest_route( 't2mchat/v2', '/get_curr_user_lang', array(
'methods' => 'GET',
'callback' => 'get_user_lang'
));
});
Run Code Online (Sandbox Code Playgroud)
当它不是基于类的方法时,我可以调用回调函数“ get_user_lang”。但是一旦将其转换为基于类的方法,便无法调用它。
我的课看起来像这样:
<?php
namespace T2mchat\TokenHandler;
class TokenHandler {
function get_user_lang() {
return "client_langs";
}
}
?>
Run Code Online (Sandbox Code Playgroud)
我的新端点如下所示:
$t2m = new T2mchat\TokenHandler\TokenHandler();
add_action( 'rest_api_init', function () {
register_rest_route( 't2mchat/v2', '/get_curr_user_lang', array(
'methods' => 'GET',
'callback' => array($t2m, 'get_user_lang')
));
});
Run Code Online (Sandbox Code Playgroud)
有人对如何在WordPress Rest API自定义终结点中调用基于类的方法有任何想法吗?