我有一个数组;
[
{pre_ref_fullname: "", pre_ref_position: "", pre_ref_phone: ""},
{pre_ref_fullname: "", pre_ref_position: "", pre_ref_phone: ""},
{pre_ref_fullname: "", pre_ref_position: "", pre_ref_phone: ""}
]
因此,如果值为空,我想删除元素。如果用户添加了2条推荐信息,我想获得此数据;
[
{pre_ref_fullname: "Name", pre_ref_position: "Job", pre_ref_phone: "Number"},
{pre_ref_fullname: "Name2", pre_ref_position: "Job2", pre_ref_phone: "Number2"}
]
我使用了一些方法,但是没有用。这是使用reduce的最后一次尝试,并且$ prerefData具有此数组;
var arr = $prerefData.reduce((result, item) => {
if (item.value != "") {
result.push(item);
}
return result;
}, []);
console.log(arr);
答案 0 :(得分:2)
您可以使用Javascript Array filter method
简而言之<TABLE>
将遍历数组,并对每个元素运行回调:
DcmDspRoutineIdentifier
public function autofill(){
ob_start();
$i_ic_num =$this -> input -> post('i_ic_num');
$arr_result = array();
print_r($i_ic_num);
if($i_ic_num <> ""){
$this -> load -> model("visitor_new_model");
$this -> db -> where($this -> visitor_new_model -> table.'.i_ic_num', $i_ic_num);
$visitor_new_info = $this -> visitor_new_model -> _get_single();
if ($visitor_new_info){
//$arr_result['i_ic_num'] = $visitor_new_info ->i_ic_num ;
$arr_result['s_name'] = $visitor_new_info ->s_name ;
$arr_result['i_contact_num'] = $visitor_new_info->i_contact_num ;
$arr_result['s_company_name'] = $visitor_new_info ->s_company_name ;
$arr_result['s_host'] = $visitor_new_info ->s_host ;
}
}
ob_end_clean();
echo json_encode($arr_result);
}
答案 1 :(得分:1)
您可以使用filter
,使用Object.values
提取单个对象的所有值。使用every
检查所有数组值是否不是空字符串。
let data = [{"pre_ref_fullname":"","pre_ref_position":"","pre_ref_phone":""},{"pre_ref_fullname":"","pre_ref_position":"","pre_ref_phone":""},{"pre_ref_fullname":"","pre_ref_position":"","pre_ref_phone":""},{"pre_ref_fullname":"Name","pre_ref_position":"Job","pre_ref_phone":"Number"},{"pre_ref_fullname":"Name2","pre_ref_position":"Job2","pre_ref_phone":"Number2"}];
let result = data.filter(o => Object.values(o).every(e => e.trim() !== ""));
console.log(result);
如果要检查至少一个不是空字符串,可以使用some
。
let data = [{"pre_ref_fullname":"","pre_ref_position":"","pre_ref_phone":""},{"pre_ref_fullname":"","pre_ref_position":"not Empty","pre_ref_phone":""},{"pre_ref_fullname":"","pre_ref_position":"","pre_ref_phone":""},{"pre_ref_fullname":"Name","pre_ref_position":"Job","pre_ref_phone":"Number"},{"pre_ref_fullname":"Name2","pre_ref_position":"Job2","pre_ref_phone":"Number2"}]
let result = data.filter(o => Object.values(o).some(e => e.trim() !== ""));
console.log(result);
答案 2 :(得分:1)
尝试
data.filter(x=> Object.values(x).join(''));
let data = [
{pre_ref_fullname: "", pre_ref_position: "", pre_ref_phone: ""},
{pre_ref_fullname: "", pre_ref_position: "", pre_ref_phone: ""},
{pre_ref_fullname: "", pre_ref_position: "", pre_ref_phone: ""},
{pre_ref_fullname: "Name", pre_ref_position: "Job", pre_ref_phone: "Number"},
{pre_ref_fullname: "Name2", pre_ref_position: "Job2", pre_ref_phone: "Number2"}
];
let r = data.filter(x=> Object.values(x).join(''));
console.log(r);
答案 3 :(得分:0)
您应该使用过滤器方法:
@reboot /var/log/server_mon.sh
*or*
* * * * * /var/log/server_mon.sh
由于空字符串返回false,因此如果这是唯一可以过滤出数组中元素的情况,则无需添加比较。
答案 4 :(得分:0)
将filter
与Object.values
和every
结合使用,并使用trim
来确保它排除空格:
const data = [{pre_ref_fullname:"",pre_ref_position:"",pre_ref_phone:""},{pre_ref_fullname:"",pre_ref_position:"",pre_ref_phone:""},{pre_ref_fullname:"",pre_ref_position:"",pre_ref_phone:""},{pre_ref_fullname:"Name",pre_ref_position:"Job",pre_ref_phone:"Number"},{pre_ref_fullname:"Name2",pre_ref_position:"Job2",pre_ref_phone:"Number2"}];
const res = data.filter(e => Object.values(e).every(s => s.trim != ""));
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }