Filter by property
Nope. Django filters operate at the database level, generating SQL. To filter based on Python properties, you have to load the object into Python to evaluate the property--and at that point, you've already done all the work to load it.
Filter object properties by key in ES6
If you have a list of allowed values, you can easily retain them in an object using:
const raw = { item1: { key: 'sdfd', value:'sdfd' }, item2: { key: 'sdfd', value:'sdfd' }, item3: { key: 'sdfd', value:'sdfd' }};
const allowed = ['item1', 'item3'];
const filtered = Object.keys(raw) .filter(key => allowed.includes(key)) .reduce((obj, key) => { obj[key] = raw[key]; return obj; }, {});
console.log(filtered);
How to filter object array based on attributes?
You can use the Array.prototype.filter
method:
var newArray = homes.filter(function (el) {
return el.price <= 1000 &&
el.sqft >= 500 &&
el.num_of_beds >=2 &&
el.num_of_baths >= 2.5;
});
Live Example:
var obj = { 'homes': [{ "home_id": "1", "price": "925", "sqft": "1100", "num_of_beds": "2", "num_of_baths": "2.0", }, { "home_id": "2", "price": "1425", "sqft": "1900", "num_of_beds": "4", "num_of_baths": "2.5", }, // ... (more homes) ... ]};// (Note that because `price` and such are given as strings in your object,// the below relies on the fact that <= and >= with a string and number// will coerce the string to a number before comparing.)var newArray = obj.homes.filter(function (el) { return el.price <= 1000 && el.sqft >= 500 && el.num_of_beds >= 2 && el.num_of_baths >= 1.5; // Changed this so a home would match});console.log(newArray);
How to filter an array of objects only by name property? - Javascript
Array.filter
will filter the array removing unwanted items, You're looking for Array.map to keep the same array length changing the value of each element of the array :
let movies = [{
'name': 'Jumanji',
'duration': 120
},
{
'name': 'Harry P',
'duration': 60
},
{
'name': 'Toy Story',
'duration': 240
}
];
let newAr = movies.map((item, index, arr) => {
return item.name
})
console.log('newAr', newAr)
How to filter an array based on property value, or existence of properties
You're looking for Object#hasOwnProperty
. Specifially, you're looking to filter
your array to only the items which have the property season
which you can accomplish like this:
const cities = [
{ "population": 121321313, "location": "American city in the East" },
{ "season": "winter", "population": 54646546, "location": "Canadian city in the East" },
{ "population": 6546467,"location": "American city in the West"},
{ "season": "fall", "population": 145313, "location": "American city in the South" },
{ "population": 12673, "location": "Canadian city2 in the East" },
{ "population": 141313, "location": "Canadian city in the South" },
{ "season": "fall", "population": 1264473, "location": "Canadian city4 in the East" },
{ "population": 12673, "location": "Canadian city6 in the South" }
];
const filteredCities = cities.filter(x => x.hasOwnProperty("season"));
console.dir(filteredCities);
Filtering an array by multiple property values
You could check if query is an array and/or the value is an array and check accordingly.
function nestedFilter(data, query) {
const
filters = Object.entries(query);
return data.filter(o => filters.every(([k, v]) => Array.isArray(v)
? Array.isArray(o[k])
? v.some(s => o[k].includes(s))
: v.includes(o[k])
: Array.isArray(o[k])
? o[k].includes(v)
: o[k] === v
));
}
const
cars = [{ name: "BMW X5", topsales: ["USA", "China", "Russia"], maxspeed: 250, users: ["teenage", "ladies", "mens"] }, { name: "Volkswagen Touareg", topsales: ["USA", "Germany"], maxspeed: 240, users: ["teenage", "mens", "old mens"] }],
query = { topsales: ["USA", "China"], users: "teenage" };
console.log(nestedFilter(cars, query));
JSONPath filter property by value in object
It looks like JSONPath expressions applies only to arrays. See here and here. Your query $.queue[?(@.size>0)]
works for:
{
"queue": [{
"size": 13
},
{
"size": 10
}]
}
Related Topics
Differencebetween Installing a Package Using Pip VS. Apt-Get
How to Directly Send a Python Output to Clipboard
Difference Between Variables Inside and Outside of _Init_()
Access an Arbitrary Element in a Dictionary in Python
Python Functions Call by Reference
Why am I Getting "Indentationerror: Expected an Indented Block"
Unboundlocalerror with Nested Function Scopes
Why Are Scripting Languages (E.G. Perl, Python, and Ruby) Not Suitable as Shell Languages
Parallel Processing from a Command Queue on Linux (Bash, Python, Ruby... Whatever)
Name This Python/Ruby Language Construct (Using Array Values to Satisfy Function Parameters)
Numpy Selecting Specific Column Index Per Row by Using a List of Indexes
How to Make a Cross-Module Variable
Converting Int to Bytes in Python 3
How to Install Python Packages on Windows