How to check the depth of every object field in Javascript - javascript

I have a navigation function in my React Native app, that outputs to the console all the arguments passed to it in the developer's mode, and sometimes i sent a big store to the arguments and it can not be output. Get the error about the cyclic object reference, because the object is very deep. Therefore I decided to create a function that will check all the fields of the object and depends on it will output the information to the console, for example if the object filed is deeper than 1 level.
const notDeepObj = {
name: 'John',
surname: 'Robert',
age: 28,
family: false,
};
const deepObj = {
name: 'John',
surname: 'Robert',
bankAccount: {
accounts: 2,
cash: true,
credit false,
wasCreated: {
city: 'New-York',
date: '12.02.2020.',
}
}
}
function checkDepthOfObject(obj){}
In the case of not deep object it has to return the object itself like this:
checkDepthOfObject(notDeepObj)
//it will return:
{
name: 'John',
surname: 'Robert',
age: 28,
family: false,
};
And in the case of the deep object it has to return all not deep fields and plus the flag for the deep field of the object:
checkDepthOfObject(notDeepObj)
//it will return:
{
name: 'John',
surname: 'Robert',
bankAccount: '[DEEP_OBJECT]'
};
Can you recommend me please the best way how can I do it.

Use Object.entries and map and check for typeof value.
const notDeepObj = {
name: "John",
surname: "Robert",
age: 28,
family: false
};
const deepObj = {
name: "John",
surname: "Robert",
bankAccount: {
accounts: 2,
cash: true,
credit: false,
wasCreated: {
city: "New-York",
date: "12.02.2020."
}
}
};
function checkDepthOfObject(obj) {
return Object.fromEntries(
Object.entries(obj).map(([key, value]) => [
key,
typeof value === "object" ? "[DEEP_OBJECT]" : value
])
);
}
console.log(checkDepthOfObject(notDeepObj));
console.log(checkDepthOfObject(deepObj));

Related

How to move similar profession people into newly created object (result) in javascript

I have one quick question, I want to move all similar professional people into a newly created object (result), I have written the code but all values are not moved.
const data = [
{
Name: 'Smith',
age: 25,
profession: 'Banker'
},
{
Name: 'Alex',
age: 28,
profession: 'IT'
},
{
Name: 'John',
age: 31,
profession: 'Banker'
},
{
Name: 'Harry',
age: 26,
profession: 'Nurse'
},
];
const result = {};
My code is here ...
const data = [ { Name: "Smith", age: 25, profession: "Banker" }, { Name: "Alex", age: 28, profession: "IT" }, { Name: "John", age: 31, profession: "Banker" }, { Name: "Harry", age: 26, profession: "Nurse" } ];
const result = {};
data.forEach(({ Name, age, profession }) => {
result[profession] = { Name, age };
});
console.log(result);
CodePen:
https://codepen.io/Sandy4405/pen/wvmLaJX
Expanding on my comment above, the inside of your forEach should be:
data.forEach(({ Name, age, profession }) => {
if (Array.isArray(result[profession])) {
result[profession].push({ Name, age })
} else {
result[profession] = [{ Name, age }]
}
});
You need to create a nested JSON, which means an array of objects for a similar profession. While iterating, create an array for each profession and use .push() to add the objects. The code would look like below
data.forEach(({Name,age,profession}) => {
result[profession] = result[profession] || [];
result[profession].push({Name,age});
});
Working Version:
const data = [{
Name: "Smith",
age: 25,
profession: "Banker"
},
{
Name: "Alex",
age: 28,
profession: "IT"
},
{
Name: "John",
age: 31,
profession: "Banker"
},
{
Name: "Harry",
age: 26,
profession: "Nurse"
}
];
const result = {};
data.forEach(({
Name,
age,
profession
}) => {
result[profession] = result[profession] || [];
result[profession].push({
Name,
age
});
});
console.log(result);
After the first person with a given profession is encountered, each subsequent person with the same profession will overwrite the previous one. You should instead create an array and push each of the people with the same profession to that array.
First, we'll check if the current profession has been encountered before.
If it hasn't, we'll create an empty array to hold all of the people with this profession.
if (!(profession in result)) {
result[profession] = []
}
Then, since the array is now guaranteed to exist for this profession, we can push to our new array the current person. The next time this profession is encountered, our first check will be skipped and we'll just push the next person onto the array.
result[profession].push({
Name,
age
})
Full example:
const data = [{
Name: "Smith",
age: 25,
profession: "Banker"
}, {
Name: "Alex",
age: 28,
profession: "IT"
}, {
Name: "John",
age: 31,
profession: "Banker"
}, {
Name: "Harry",
age: 26,
profession: "Nurse"
}]
const result = {}
data.forEach(({
Name,
age,
profession
}) => {
if (!(profession in result))
result[profession] = []
result[profession].push({
Name,
age
})
})
console.log(result)
As filipe said in his comment, Your current code is just assigning the values into an object which results in maintaining the single value against keys. To group the same profession values into a respective profession key, You have to do something like this :
const data = [ { Name: "Smith", age: 25, profession: "Banker" }, { Name: "Alex", age: 28, profession: "IT" }, { Name: "John", age: 31, profession: "Banker" }, { Name: "Harry", age: 26, profession: "Nurse" } ];
const result = {};
data.forEach(({ Name, age, profession }) => {
result[profession] ? result[profession].push({ Name, age }) : result[profession] = [{ Name, age }];
});
console.log(result);

Map object property only when it is defined (not null) [duplicate]

This question already has answers here:
In JavaScript, how to conditionally add a member to an object?
(29 answers)
Add property to object when it's not null
(3 answers)
Closed 1 year ago.
Let’s consider the next array:
const arr = [
{
name: "bob",
age: 25,
salary: 1000
},
{
name: "bill",
age: 32,
salary: 1500
},
{
name: "jake",
age: 16,
salary: null
},
]
I need to map every object to be the next structure:
firstName: string;
personAge: string;
grossSalary?: number;
so
const mappedArr = arr.map(person => ({
firstName: person.name,
personAge: person.age,
...{grossSalary:
person.salary
? person.salary
: // I'm stuck :'((
}
}))
I need to map person.salary only if it’s not null in the original object. Otherwise, I need to omit it.
I believe I’m pretty close with the spread operator but I guess I need a ternary to return an empty object if the salary is null in the original object. Maybe this approach is wrong... idk anymore...
You can check the salary based on which you can return the object:
const arr = [
{
name: "bob",
age: 25,
salary: 1000
},
{
name: "bill",
age: 32,
salary: 1500
},
{
name: "jake",
age: 16,
salary: null
},
]
const mappedArr = arr.map(person => (
person.salary
? { firstName: person.name, personAge: person.age, grossSalary: person.salary }
: { firstName: person.name, personAge: person.age }
)
);
console.log(mappedArr);

Check if objects in array contain equal properties

I have an array of objects, each with various properties. I want to check if one particular property is equal across all of the objects. e.g.
peopleArr = [
{
name: Simon,
age: 22,
hair: brown
},
{
name: John,
age: 22,
hair: black
},
{
name: James,
age: 22,
hair: blond
}
]
I need a function that returns true if age has the same value across all of the objects in the array, and false if not. I've tried some variations using .every, but can't get it to work with object properties specifically (I'm relatively new). Any help appreciated.
You can use array every method and inside the callback check if age in all the object is equal to 22. It will return Boolean value and it will return true if all all the object matches the condition
const peopleArr = [{
name: 'Simon',
age: 22,
hair: 'brown'
},
{
name: 'John',
age: 22,
hair: 'black'
},
{
name: 'James',
age: 23,
hair: 'blond'
}
]
const res = peopleArr.every(item => item.age === 22);
console.log(res)
An alternative to using the .every() method, would be to filter the array and compare the filtered array length to the original array length. Like so
const peopleArr = [
{
name: "Simon",
age: 22,
hair: "brown"
},
{
name: "John",
age: 22,
hair: "black"
},
{
name: "James",
age: 22,
hair: "blond"
}
]
const array_val_same = ( arr, val ) => {
let filtered = arr.filter(el => el.age === val)
return filtered.length === arr.length ? true : false
}
array_val_same(peopleArr, 22)
This is just an alternative, i'd still use .every() though.

Using Javascript Array Properties to restrict a random output [duplicate]

I want to simplify an array of objects. Let's assume that I have following array:
var users = [{
name: 'John',
email: 'johnson#mail.com',
age: 25,
address: 'USA'
},
{
name: 'Tom',
email: 'tom#mail.com',
age: 35,
address: 'England'
},
{
name: 'Mark',
email: 'mark#mail.com',
age: 28,
address: 'England'
}];
And filter object:
var filter = {address: 'England', name: 'Mark'};
For example i need to filter all users by address and name, so i do loop through filter object properties and check it out:
function filterUsers (users, filter) {
var result = [];
for (var prop in filter) {
if (filter.hasOwnProperty(prop)) {
//at the first iteration prop will be address
for (var i = 0; i < filter.length; i++) {
if (users[i][prop] === filter[prop]) {
result.push(users[i]);
}
}
}
}
return result;
}
So during first iteration when prop - address will be equal 'England' two users will be added to array result (with name Tom and Mark), but on the second iteration when prop name will be equal Mark only the last user should be added to array result, but i end up with two elements in array.
I have got a little idea as why is it happening but still stuck on it and could not find a good solution to fix it. Any help is appreciable. Thanks.
You can do like this
var filter = {
address: 'England',
name: 'Mark'
};
var users = [{
name: 'John',
email: 'johnson#mail.com',
age: 25,
address: 'USA'
},
{
name: 'Tom',
email: 'tom#mail.com',
age: 35,
address: 'England'
},
{
name: 'Mark',
email: 'mark#mail.com',
age: 28,
address: 'England'
}
];
users= users.filter(function(item) {
for (var key in filter) {
if (item[key] === undefined || item[key] != filter[key])
return false;
}
return true;
});
console.log(users)
If you know the name of the filters, you can do it in a line.
users = users.filter(obj => obj.name == filter.name && obj.address == filter.address)
Another take for those of you that enjoy succinct code.
NOTE: The FILTER method can take an additional this argument, then using an E6 arrow function we can reuse the correct this to get a nice one-liner.
var users = [{name: 'John',email: 'johnson#mail.com',age: 25,address: 'USA'},
{name: 'Tom',email: 'tom#mail.com',age: 35,address: 'England'},
{name: 'Mark',email: 'mark#mail.com',age: 28,address: 'England'}];
var query = {address: "England", name: "Mark"};
var result = users.filter(search, query);
function search(user){
return Object.keys(this).every((key) => user[key] === this[key]);
}
// |----------------------- Code for displaying results -----------------|
var element = document.getElementById('result');
function createMarkUp(data){
Object.keys(query).forEach(function(key){
var p = document.createElement('p');
p.appendChild(document.createTextNode(
key.toUpperCase() + ': ' + result[0][key]));
element.appendChild(p);
});
}
createMarkUp(result);
<div id="result"></div>
Here is ES6 version of using arrow function in filter. Posting this as an answer because most of us are using ES6 these days and may help readers to do filter in advanced way using arrow function, let and const.
const filter = {
address: 'England',
name: 'Mark'
};
let users = [{
name: 'John',
email: 'johnson#mail.com',
age: 25,
address: 'USA'
},
{
name: 'Tom',
email: 'tom#mail.com',
age: 35,
address: 'England'
},
{
name: 'Mark',
email: 'mark#mail.com',
age: 28,
address: 'England'
}
];
users= users.filter(item => {
for (let key in filter) {
if (item[key] === undefined || item[key] != filter[key])
return false;
}
return true;
});
console.log(users)
users.filter(o => o.address == 'England' && o.name == 'Mark')
Much better for es6. or you can use || (or) operator like this
users.filter(o => {return (o.address == 'England' || o.name == 'Mark')})
Can also be done this way:
this.users = this.users.filter((item) => {
return (item.name.toString().toLowerCase().indexOf(val.toLowerCase()) > -1 ||
item.address.toLowerCase().indexOf(val.toLowerCase()) > -1 ||
item.age.toLowerCase().indexOf(val.toLowerCase()) > -1 ||
item.email.toLowerCase().indexOf(val.toLowerCase()) > -1);
})
Using Array.Filter() with Arrow Functions we can achieve this using
users = users.filter(x => x.name == 'Mark' && x.address == 'England');
Here is the complete snippet
// initializing list of users
var users = [{
name: 'John',
email: 'johnson#mail.com',
age: 25,
address: 'USA'
},
{
name: 'Tom',
email: 'tom#mail.com',
age: 35,
address: 'England'
},
{
name: 'Mark',
email: 'mark#mail.com',
age: 28,
address: 'England'
}
];
//filtering the users array and saving
//result back in users variable
users = users.filter(x => x.name == 'Mark' && x.address == 'England');
//logging out the result in console
console.log(users);
Improving on the good answers here, below is my solution:
const rawData = [
{ name: 'John', email: 'johnson#mail.com', age: 25, address: 'USA' },
{ name: 'Tom', email: 'tom#mail.com', age: 35, address: 'England' },
{ name: 'Mark', email: 'mark#mail.com', age: 28, address: 'England' }
]
const filters = { address: 'England', age: 28 }
const filteredData = rawData.filter(i =>
Object.entries(filters).every(([k, v]) => i[k] === v)
)
I think this might help.
const filters = ['a', 'b'];
const results = [
{
name: 'Result 1',
category: ['a']
},
{
name: 'Result 2',
category: ['a', 'b']
},
{
name: 'Result 3',
category: ['c', 'a', 'b', 'd']
}
];
const filteredResults = results.filter(item =>
filters.every(val => item.category.indexOf(val) > -1)
);
console.log(filteredResults);
Dynamic filters with AND condition
Filter out people with gender = 'm'
var people = [
{
name: 'john',
age: 10,
gender: 'm'
},
{
name: 'joseph',
age: 12,
gender: 'm'
},
{
name: 'annie',
age: 8,
gender: 'f'
}
]
var filters = {
gender: 'm'
}
var out = people.filter(person => {
return Object.keys(filters).every(filter => {
return filters[filter] === person[filter]
});
})
console.log(out)
Filter out people with gender = 'm' and name = 'joseph'
var people = [
{
name: 'john',
age: 10,
gender: 'm'
},
{
name: 'joseph',
age: 12,
gender: 'm'
},
{
name: 'annie',
age: 8,
gender: 'f'
}
]
var filters = {
gender: 'm',
name: 'joseph'
}
var out = people.filter(person => {
return Object.keys(filters).every(filter => {
return filters[filter] === person[filter]
});
})
console.log(out)
You can give as many filters as you want.
In lodash,
_.filter(users,{address: 'England', name: 'Mark'})
In es6,
users.filter(o => o.address == 'England' && o.name == 'Mark')
You'll have more flexibility if you turn the values in your filter object into arrays:
var filter = {address: ['England'], name: ['Mark'] };
That way you can filter for things like "England" or "Scotland", meaning that results may include records for England, and for Scotland:
var filter = {address: ['England', 'Scotland'], name: ['Mark'] };
With that setup, your filtering function can be:
const applyFilter = (data, filter) => data.filter(obj =>
Object.entries(filter).every(([prop, find]) => find.includes(obj[prop]))
);
// demo
var users = [{name: 'John',email: 'johnson#mail.com',age: 25,address: 'USA'},{name: 'Tom',email: 'tom#mail.com',age: 35,address: 'England'},{name: 'Mark',email: 'mark#mail.com',age: 28,address: 'England'}];var filter = {address: ['England'], name: ['Mark'] };
var filter = {address: ['England'], name: ['Mark'] };
console.log(applyFilter(users, filter));
If you want to put multiple conditions in filter, you can use && and || operator.
var product= Object.values(arr_products).filter(x => x.Status==status && x.email==user)
A clean and functional solution
const combineFilters = (...filters) => (item) => {
return filters.map((filter) => filter(item)).every((x) => x === true);
};
then you use it like so:
const filteredArray = arr.filter(combineFilters(filterFunc1, filterFunc2));
and filterFunc1 for example might look like this:
const filterFunc1 = (item) => {
return item === 1 ? true : false;
};
We can use different operators to provide multiple condtion to filter the array in the following way
Useing OR (||) Operator:
const orFilter = [{a:1, b: 3}, {a:1,b:2}, {a: 2, b:2}].filter(d => (d.a !== 1 || d.b !== 2))
console.log(orFilter, 'orFilter')
Using AND (&&) Operator:
const andFilter = [{a:1, b: 3}, {a:1,b:2}, {a: 2, b:2}].filter(d => (d.a !== 1 && d.b !== 2))
console.log(andFilter, 'andFilter')
functional solution
function applyFilters(data, filters) {
return data.filter(item =>
Object.keys(filters)
.map(keyToFilterOn =>
item[keyToFilterOn].includes(filters[keyToFilterOn]),
)
.reduce((x, y) => x && y, true),
);
}
this should do the job
applyFilters(users, filter);
My solution, based on NIKHIL C M solution:
let data = [
{
key1: "valueA1",
key2: "valueA2",
key3: []
},{
key1: "valueB1",
key2: "valueB2"
key3: ["valuesB3"]
}
];
let filters = {
key1: "valueB1",
key2: "valueB2"
};
let filteredData = data.filter((item) => {
return Object.entries(filters).every(([filter, value]) => {
return item[filter] === value;
//Here i am applying a bit more logic like
//return item[filter].includes(value)
//or filter with not exactly same key name like
//return !isEmpty(item.key3)
});
});
A question I was in the middle of answering got (properly) closed as duplicate of this. But I don't see any of the answers above quite like this one. So here's one more option.
We can write a simple function that takes a specification such as {name: 'mike', house: 'blue'}, and returns a function that will test if the value passed to it matches all the properties. It could be used like this:
const where = (spec, entries = Object .entries (spec)) => (x) =>
entries .every (([k, v]) => x [k] == v)
const users = [{name: 'John', email: 'johnson#mail.com', age: 25, address: 'USA'}, {name: 'Mark', email: 'marcus#mail.com', age: 25, address: 'USA'}, {name: 'Tom', email: 'tom#mail.com', age: 35, address: 'England'}, {name: 'Mark', email: 'mark#mail.com', age: 28, address: 'England'}]
console .log ('Mark', users .filter (where ({name: 'Mark'})))
console .log ('England', users .filter (where ({address: 'England'})))
console .log ('Mark/England', users .filter (where ({name: 'Mark', address: 'England'})))
.as-console-wrapper {max-height: 100% !important; top: 0}
And if we wanted to wrap the filtering into a single function, we could reuse that same function, wrapped up like this:
const where = (spec, entries = Object .entries (spec)) => (x) =>
entries .every (([k, v]) => x [k] == v)
const filterBy = (spec) => (xs) =>
xs .filter (where (spec))
const users = [{name: 'John', email: 'johnson#mail.com', age: 25, address: 'USA'}, {name: 'Mark', email: 'marcus#mail.com', age: 25, address: 'USA'}, {name: 'Tom', email: 'tom#mail.com', age: 35, address: 'England'}, {name: 'Mark', email: 'mark#mail.com', age: 28, address: 'England'}]
console .log ('Mark/England', filterBy ({address: "England", name: "Mark"}) (users))
.as-console-wrapper {max-height: 100% !important; top: 0}
(Of course that last doesn't have to be curried. We could change that so that we could call it with two parameters at once. I find this more flexible, but YMMV.)
Keeping it as a separate function has the advantage that we could then reuse it, in say, a find or some other matching situation.
This design is very similar to the use of where in Ramda (disclaimer: I'm one of Ramda's authors.) Ramda offers the additional flexibility of allowing arbitrary predicates instead of values that have to be equal. So in Ramda, you might write something like this instead:
filter (where ({
address: equals ('England')
age: greaterThan (25)
}) (users)
It's much the same idea, only a bit more flexible.
If the finality of you code is to get the filtered user, I would invert the for to evaluate the user instead of reducing the result array during each iteration.
Here an (untested) example:
function filterUsers (users, filter) {
var result = [];
for (i=0;i<users.length;i++){
for (var prop in filter) {
if (users.hasOwnProperty(prop) && users[i][prop] === filter[prop]) {
result.push(users[i]);
}
}
}
return result;
}
with the composition of some little helpers:
const filter = {address: 'England', name: 'Mark'};
console.log(
users.filter(and(map(propMatches)(filter)))
)
function propMatches<T>(property: string, value: any) {
return (item: T): boolean => item[property] === value
}
function map<T>(mapper: (key: string, value: any, obj: T) => (item:T) => any) {
return (obj: T) => {
return Object.keys(obj).map((key) => {
return mapper(key, obj[key], obj)
});
}
}
export function and<T>(predicates: ((item: T) => boolean)[]) {
return (item: T) =>
predicates.reduce(
(acc: boolean, predicate: (item: T) => boolean) => {
if (acc === undefined) {
return !!predicate(item);
}
return !!predicate(item) && acc;
},
undefined // initial accumulator value
);
}
This is an easily understandable functional solution
let filtersObject = {
address: "England",
name: "Mark"
};
let users = [{
name: 'John',
email: 'johnson#mail.com',
age: 25,
address: 'USA'
},
{
name: 'Tom',
email: 'tom#mail.com',
age: 35,
address: 'England'
},
{
name: 'Mark',
email: 'mark#mail.com',
age: 28,
address: 'England'
}
];
function filterUsers(users, filtersObject) {
//Loop through all key-value pairs in filtersObject
Object.keys(filtersObject).forEach(function(key) {
//Loop through users array checking each userObject
users = users.filter(function(userObject) {
//If userObject's key:value is same as filtersObject's key:value, they stay in users array
return userObject[key] === filtersObject[key]
})
});
return users;
}
//ES6
function filterUsersES(users, filtersObject) {
for (let key in filtersObject) {
users = users.filter((userObject) => userObject[key] === filtersObject[key]);
}
return users;
}
console.log(filterUsers(users, filtersObject));
console.log(filterUsersES(users, filtersObject));
This is another method i figured out, where filteredUsers is a function that returns the sorted list of users.
var filtersample = {address: 'England', name: 'Mark'};
filteredUsers() {
return this.users.filter((element) => {
return element['address'].toLowerCase().match(this.filtersample['address'].toLowerCase()) || element['name'].toLowerCase().match(this.filtersample['name'].toLowerCase());
})
}
const users = [{
name: 'John',
email: 'johnson#mail.com',
age: 25,
address: 'USA'
},
{
name: 'Tom',
email: 'tom#mail.com',
age: 35,
address: 'England'
},
{
name: 'Mark',
email: 'mark#mail.com',
age: 28,
address: 'England'
}
];
const filteredUsers = users.filter(({ name, age }) => name === 'Tom' && age === 35)
console.log(filteredUsers)
Using lodash and not pure javascript
This is actually quite simple using lodash and very easy to add/modify filters.
import _ from 'lodash';
async getUsersWithFilter(filters) {
const users = yourArrayOfSomethingReally();
// Some properties of the 'filters' object can be null or undefined, so create a new object without those undefined properties and filter by those who are defined
const filtersWithoutUndefinedValuesObject = _.omitBy(
filters,
_.isNil,
);
return _.filter(users, { ...filtersWithoutUndefinedValuesObject });
}
The omitBy function checks your filters object and removes any value that is null or undefined (if you take it out, the lodash.filter function wont return any result.
The filter function will filter out all the objects who's values don't match with the object you pass as a second argument to the function (which in this case, is your filters object.)
Why use this?
Well, assume you have this object:
const myFiltersObj = {
name: "Java",
age: 50
};
If you want to add another filter, just add a new property to the myFilterObj, like this:
const myFiltersObj = {
name: "Java",
email: 50,
country: "HND"
};
Call the getUsersWithFilter function, and it will work just fine. If you skip, let's say the name property in the object, the getUsersWithFilter function will filter by the email and country just fine.
Please check below code snippet with data you provided, it will return filtered data on the basis of multiple columns.
var filter = {
address: 'India',
age: '27'
};
var users = [{
name: 'Nikhil',
email: 'nikhil#mail.com',
age: 27,
address: 'India'
},
{
name: 'Minal',
email: 'minal#mail.com',
age: 27,
address: 'India'
},
{
name: 'John',
email: 'johnson#mail.com',
age: 25,
address: 'USA'
},
{
name: 'Tom',
email: 'tom#mail.com',
age: 35,
address: 'England'
},
{
name: 'Mark',
email: 'mark#mail.com',
age: 28,
address: 'England'
}
];
function filterByMultipleColumns(users, columnDataToFilter) {
return users.filter(row => {
return Object.keys(columnDataToFilter).every(propertyName => row[propertyName].toString().toLowerCase().indexOf(columnDataToFilter[propertyName].toString().toLowerCase()) > -1);
})
}
var filteredData = filterByMultipleColumns(users, filter);
console.log(filteredData);
Result :
[ { "name": "Nikhil", "email": "nikhil#mail.com", "age": 27, "address": "India" }, { "name": "Minal", "email": "minal#mail.com", "age": 27, "address": "India" } ]
Please check below link which can used with just small changes
Javascript filter array multiple values – example
const data = [{
realName: 'Sean Bean',
characterName: 'Eddard “Ned” Stark'
}, {
realName: 'Kit Harington',
characterName: 'Jon Snow'
}, {
realName: 'Peter Dinklage',
characterName: 'Tyrion Lannister'
}, {
realName: 'Lena Headey',
characterName: 'Cersei Lannister'
}, {
realName: 'Michelle Fairley',
characterName: 'Catelyn Stark'
}, {
realName: 'Nikolaj Coster-Waldau',
characterName: 'Jaime Lannister'
}, {
realName: 'Maisie Williams',
characterName: 'Arya Stark'
}];
const filterKeys = ['realName', 'characterName'];
const multiFilter = (data = [], filterKeys = [], value = '') => data.filter((item) => filterKeys.some(key => item[key].toString().toLowerCase().includes(value.toLowerCase()) && item[key]));
let filteredData = multiFilter(data, filterKeys, 'stark');
console.info(filteredData);
/* [{
"realName": "Sean Bean",
"characterName": "Eddard “Ned” Stark"
}, {
"realName": "Michelle Fairley",
"characterName": "Catelyn Stark"
}, {
"realName": "Maisie Williams",
"characterName": "Arya Stark"
}]
*/
arr.filter((item) => {
if(condition)
{
return false;
}
return true;
});

Function that returns specified values, from an array of objects, if a property is true

Im working on a challenge: Given an array of objects, write a function admin that returns the name and birthdate of all users that are marked as "admin".
const users = [
{
name: 'Homer',
role: 'clerk',
dob: '12/02/1988',
admin: false
},
{
name: 'Lisa',
role: 'staff',
dob: '01/30/1965',
admin: false
},
{
name: 'Marge',
role: 'associate',
dob: '09/10/1980',
admin: true
}
]
function admin(myObj) {
function groupBy(myObj, prop) {
return myObj.reduce(function (acc, obj) {
let key = obj[prop]
if (!acc[key]) {
acc[key] = []
}
acc[key].push(obj)
return acc
}, {});
}
let trueAdmin = groupBy(myObj, 'admin');
console.log(trueAdmin[true].map(({name, dob}) => `Name: ${name}
Dob: ${dob}
`).join(`
`));
}
admin(users);
When I run tests, it comes back as undefined and i do not know why. Im looking for an expected output:
admin(users)
// Name: Marge
// Dob: 09/10/1980
If you just want to log them to the console you can simply use forEach()
const users = [
{
name: 'Homer',
role: 'clerk',
dob: '12/02/1988',
admin: false
},
{
name: 'Lisa',
role: 'staff',
dob: '01/30/1965',
admin: false
},
{
name: 'Marge',
role: 'associate',
dob: '09/10/1980',
admin: true
}
]
function admin(myArray) {
myArray.forEach(user => {
if (user.admin) console.log(`Name: ${user.name}, DOB: ${user.dob}`);
});
}
admin(users);
If you want to return the matched users you can use filter() to return an array filtered by a callback, and then map() that to alter the elements of the filtered array so they conform to the expected output.
In the following snippet admin() returns an array of mutated user objects with only name and dob keys.
const users = [
{
name: 'Homer',
role: 'clerk',
dob: '12/02/1988',
admin: false
},
{
name: 'Lisa',
role: 'staff',
dob: '01/30/1965',
admin: false
},
{
name: 'Marge',
role: 'associate',
dob: '09/10/1980',
admin: true
}
]
function admin(myArray) {
return myArray.filter(u => u.admin).map(u => ({name: u.name, dob: u.dob}));
}
console.log(admin(users));
filter(u => u.admin) iterates over each object and returns an array containing elements for which user.admin is true.
map(u => ({name: u.name, dob: u.dob})) then iterates over each element of the filtered array and changes each one to be an object containing just name: and dob: of the passed object.

Categories