Filter by Property

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



Leave a reply



Submit