在多个条件下过滤对象数组

时间:2019-07-03 09:45:50

标签: javascript ecmascript-6 lodash

我有这个数组

const d = [{
    "type": "price",
    "value": {
        "min": 0,
        "max": 170
    }
}, {
    "type": "name",
    "value": {}
}, {
    "type": "volume",
    "options": [1,2]
}]

如果值没有值或选项为空数组,我想过滤。所以我做到了

d.filter(o => o.value || o.options)

我希望type:name消失了,但为什么它仍然存在?

我也尝试过lodash

d.filter(o => !isEmpty(o.value) || !isEmpty(o.options))

不能按预期工作吗?

5 个答案:

答案 0 :(得分:2)

{}[]是真实值

console.log(Boolean([]))
console.log(Boolean({}))

如果是数组,则可以检查长度;如果是对象,则可以获取键,值或条目并检查其长度

const d = [{"type": "price","value": {"min": 0,"max": 170}}, {"type": "name","value": {}}, {"type": "volume","options": [1,2]}]

let op = d.filter(o => ( Object.values(o.value || {}) || (o.options || [] )).length)

console.log(op)

答案 1 :(得分:0)

{}仍然是一个真实值,您必须检查其键的长度

const d = [{
    "type": "price",
    "value": {
        "min": 0,
        "max": 170
    }
}, {
    "type": "name",
    "value": {}
}, {
    "type": "volume",
    "options": [1,2]
}]

console.log(d.filter(o => (o.value && Object.keys(o.value).length) || o.options))

答案 2 :(得分:0)

这是我的解决方案。

const d = [{
    "type": "price",
    "value": {
        "min": 0,
        "max": 170
    }
}, {
    "type": "name",
    "value": {}
}, {
    "type": "volume",
    "options": [1,2]
}]

function notEmpty(n){
   return (!!n ? typeof n === 'object' ? Array.isArray(n) ? !!n.length : !!Object.keys(n).length : true : false);
}

console.log(d.filter(o => notEmpty(o.value) || notEmpty(o.options)));

希望会为您提供帮助。

答案 3 :(得分:0)

提供自定义过滤器回调,您可以在其中检查values属性和options属性中的任何entry

const d = [
	{
		"type": "price",
		"value": {
			"min": 0,
			"max": 170
		}
	}, 
	{
		"type": "name",
		"value": {}
	}, 
	{
		"type": "volume",
		"options": [1,2]
	}
];


function doesHaveValuesAndOptions(obj) {
	const valueEntries = obj.value ? Object.entries(obj.value) : [],
		    options = obj.options || [];
	
	if (!valueEntries.length && !options.length)
		return false;
	
	else if (valueEntries.length || options.length)
		return true;
	
	return false;
}



const res = d.filter(doesHaveValuesAndOptions);
console.log(res);

答案 4 :(得分:0)

使用lodash,您可以将# Proxy for interactive terminals echo "http_proxy=http://PROXY_ADDRESS:PROXY_PORT" >> /etc/environment echo "https_proxy=http://PROXY_ADDRESS:PROXY_PORT" >> /etc/environment echo "no_proxy=127.0.0.1,localhost" >> /etc/environment # Proxy for daemons (e.g. Docker deamon - used to pull images, apt - run from default daily cron job) mkdir /etc/systemd/system.conf.d echo [Manager] > /etc/systemd/system.conf.d/01-http-proxy.conf echo "DefaultEnvironment=\"http_proxy=PROXY_ADDRESS:PROXY_PORT\"" >> /etc/systemd/system.conf.d/01-http-proxy.conf echo "DefaultEnvironment=\"https_proxy=PROXY_ADDRESS:PROXY_PORT\"" >> /etc/systemd/system.conf.d/01-http-proxy.conf echo "DefaultEnvironment=\"no_proxy=127.0.0.1,localhost\"" >> /etc/systemd/system.conf.d/01-http-proxy.conf echo "# Docker requires upper-case http proxy environment variables..." >> /etc/systemd/system.conf.d/01-http-proxy.conf echo "DefaultEnvironment=\"HTTP_PROXY=http://PROXY_ADDRESS:PROXY_PORT2\"" >> /etc/systemd/system.conf.d/01-http-proxy.conf echo "DefaultEnvironment=\"HTTPS_PROXY=http://PROXY_ADDRESS:PROXY_PORT\"" >> /etc/systemd/system.conf.d/01-http-proxy.conf echo "DefaultEnvironment=\"NO_PROXY=127.0.0.1,localhost\"" >> /etc/systemd/system.conf.d/01-http-proxy.conf # Proxy for docker containers started with `docker run` mkdir /home/vagrant/.docker cat <<EOF > /home/vagrant/.docker/config.json { "proxies": { "default": { "httpProxy": "http:/PROXY_ADDRESS:PROXY_PORT", "httpsProxy": "http://PROXY_ADDRESS:PROXY_PORT", "noProxy": "127.0.0.1,localhost" } } } EOF chown -R vagrant:vagrant /home/vagrant/.docker $resultJson = array(); $resultJson['osoby']=array() $query = "SELECT imie,nazwisko FROM users"; $result = $mysql->query( $query ); if ($result->num_rows > 0){ while($row = $result->fetch_assoc()){ // fetch information out of the $row.. $resultJson['osoby'][] = ['name' => $row['imie'], 'surname' => $row['nazwisko']]; } } print json_encode($resultJson); 一起使用,以删除具有空_.reject()对象和空_.isEmpty()数组的项目:

value
options

使用lodash / fp,您可以轻松生成一个函数,该函数提取const arr = [{"type": "price","value": {"min": 0,"max": 170}}, {"type": "name","value": {}}, {"type": "volume","options": [1,2]}] const result = _.reject(arr, o => _.isEmpty(o.value) && _.isEmpty(o.options)) console.log(result)<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>,并用value进行迭代,然后返回options(应删除),如果两者都是空的:

_.every()
true

相关问题