I want to filter this object
var tabledata = [
0: {Email: "user1@test.com", APP1: true, APP2: false, APP3: false, APP4: true}
]
i want only the value EMAIL and TRUE visible like this.
var tabledata = [
0: {"Email":"user1@test.com","APP1":true,"APP4":true}
]
or like this
var tabledata = {"Email":"user1@test.com","APP1":true,"APP4":true}
You can do like this.
var tabledata-2 = {}
for (key : tabledata[0].keys()) {
if (key === 'Email' || tabledata[0][key]) {
tabledata-2[key] = tabledata[0][key];
}
}
You could remap your tabledata
like that:
let tabledata = [ {Email: "user1@test.com", APP1: true, APP2: false, APP3: false, APP4: true}, {Email: "abc@you.com", APP1: false, APP2: false, APP3: true, APP4: false}, ]; let result = tabledata.map((item) => { let dict = {}; for (key in item) { if (item[key]) { dict[key] = item[key] } } return dict; }) console.log(result)
This is not a valid syntax in JavaScript:
var tabledata = [
0: {Email: "user1@test.com", APP1: true, APP2: false, APP3: false, APP4: true}
]
I assume you mean that tabledata is an object:
var tabledata = {
0: {Email: "user1@test.com", APP1: true, APP2: false, APP3: false, APP4: true}
}
Also JavaScript wise it doesn't make a difference if you write:
{"Email": "..."}
or
{Email: "..."}
as long as the object key does not contain special characters.
If you want to map your object in a functional way, you use reduce and map:
Object.values(tabledata).map(entry => {
// Remove values that are false.
const filteredProperties = Object.entries(entry)
.reduce((acc, [key, value]) => value !== false ? [...acc, [key, value]] : acc, []);
return Object.fromEntries(filteredProperties);
})
This would then give you:
[
{Email: "user1@test.com", APP1: true, APP4: true}
]
If tabledata is an array, the job is a bit simpler btw (I'm not sure from your post if tabledata really is an object, or if you copied the line from the console):
tabledata.map(entry => {
const filteredProperties = Object.entries(entry)
.reduce((acc, [key, value]) => value !== false ? [...acc, [key, value]] : acc, []);
return Object.fromEntries(filteredProperties);
})
this seems to work to.
var tabledata_2 = {}
let objects = Object.values(tabledata)
for(var i=0; i<objects.length; i++) {
for(var key in objects[i]) {
if (!!objects[i][key]) {
tabledata_2[key] = tabledata[0][key];
}
}
console.log(tabledata_2)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.