How to get last JSON object from JSONArray for given value? - javascript

I have JSONArray like given below
[
{
"id": 1,
"firstName": "abc",
"isActive": true
},
{
"id": 2,
"firstName": "cde",
"isActive": false
},
{
"id": 3,
"firstName": "efg",
"isActive": true
},
{
"id": 4,
"firstName": "ghi",
"isActive": false
}
]
Now I want to get the last object from this array who have value "isActive": true using the most effective and optimized way.
{
"id": 3,
"firstName": "efg",
"isActive": true
}
UPDATE :
let getLast = items.filter(item => item.isActive === true);
I tried using the solution provided by #brk. But is there any way to get object using without creating a new array.

First filter with condition isActive === true. This will create a new array , then use array.length-1 to get the last element
let data = [{
"id": 1,
"firstName": "abc",
"isActive": true
},
{
"id": 2,
"firstName": "cde",
"isActive": false
},
{
"id": 3,
"firstName": "efg",
"isActive": true
},
{
"id": 4,
"firstName": "ghi",
"isActive": false
}
]
let getLast = data.filter(item => item.isActive === true);
console.log(getLast[getLast.length - 1])

var jsonObject = [
{
"id": 1,
"firstName": "abc",
"isActive": true
},
{
"id": 2,
"firstName": "cde",
"isActive": false
},
{
"id": 3,
"firstName": "efg",
"isActive": true
},
{
"id": 4,
"firstName": "ghi",
"isActive": false
}
];
var lastActiveElement = jsonObject.filter( obj => obj.isActive ).pop();
console.log(lastActiveElement);
If this is what you are looking for

var x=[
{
"id": 1,
"firstName": "abc",
"isActive": true
},
{
"id": 2,
"firstName": "cde",
"isActive": false
},
{
"id": 3,
"firstName": "efg",
"isActive": true
},
{
"id": 4,
"firstName": "ghi",
"isActive": false
}
];
//Try the following line
x.filter(x=>x.isActive).slice(-1).pop()

Related

Find multiple elements in nested array

I Need to find multiple elements in a nested array and return it as an array.
I use the reduce function to find the elements, but it returns only one record.
Nested array:
{
"type": "group",
"level": 0,
"expand": "-closed",
"selected": false,
"text": "Федулов Владислав Владиславович",
"phoneNumber": "+7 (927) 999 9999",
"email": "qweeqwe#mail.ru",
"id": 24,
"parent": null,
"cardType": 0,
"childrens": [
{
"type": "group",
"level": 1,
"expand": "-closed",
"selected": false,
"text": "Ширяев Феликс Богуславович",
"phoneNumber": "+7 (123) 456 7810",
"email": "test#test.ru",
"id": 47,
"parent": 24,
"cardType": 0,
"childrens": [
{
"type": "manager",
"level": 2,
"expand": "-empty",
"selected": false,
"text": "Колесова Анастасия Олеговна",
"phoneNumber": "+7 (900) 000 0001",
"email": "eprosvirina#baccasoft.ru",
"id": 58,
"parent": 47,
"cardType": 0,
"childrens": null
}
]
}
]
},
{
"type": "group",
"level": 0,
"expand": "-closed",
"selected": false,
"text": "Игнатьева Женевьева Павловна",
"phoneNumber": "+7 (777) 777 7777",
"email": "igp#sks.ru",
"id": 3,
"parent": null,
"cardType": 0,
"childrens": [
{
"type": "group",
"level": 1,
"expand": "-closed",
"selected": false,
"text": "Меретин Викентий Васильевич",
"phoneNumber": "+7 (917) 193 5222",
"email": "keshman#gmail.com",
"id": 2,
"parent": 3,
"cardType": 1,
"childrens": [
{
"type": "manager",
"level": 2,
"expand": "-empty",
"selected": false,
"text": "Климаков Алексей Александрович",
"phoneNumber": "+7 (903) 888 8888",
"email": "krenog#gmail.com",
"id": 20,
"parent": 2,
"cardType": 1,
"childrens": null
}
]
}
]
}
and the reduce function:
var array = store.managersTree.treeNodes;
var items = [];
const findItemNested = (arr, searchString, nestingKey) => (
arr.reduce((a, item) => {
if (a) return a;
if (item.text.indexOf(searchString)!==-1 ||
item.phoneNumber.indexOf(searchString)!==-1 ||
item.email.indexOf(searchString)!==-1) return item;
if (item[nestingKey]) return findItemNested(item[nestingKey],
searchString, nestingKey)
}, [])
);
const element = findItemNested(array, searchString, "childrens");
I am trying to find a record matching at least one criteria, expecting that reduce returns multiple records, but this returns only one record, despite there were multiple records found.
Any help would be appreciated.
UPD: searchString could be string like phoneNumber, text or email
You could reduce the array recursively.
Destructure the object in reduce parameter to get nestingKey and other properties separately
Create an array of filterKeys which have the keys which you want to search for searchString.
Use some to check if any of the fields in the object have a value which includes the searchString.
If nestingKey exists, you can push the nested items to the accumulator array.
const input=[{"type":"group","level":0,"expand":"-closed","selected":false,"text":"Федулов Владислав Владиславович","phoneNumber":"+7 (927) 999 9999","email":"qweeqwe#mail.ru","id":24,"parent":null,"cardType":0,"childrens":[{"type":"group","level":1,"expand":"-closed","selected":false,"text":"Ширяев Феликс Богуславович","phoneNumber":"+7 (123) 456 7810","email":"test#test.ru","id":47,"parent":24,"cardType":0,"childrens":[{"type":"manager","level":2,"expand":"-empty","selected":false,"text":"Колесова Анастасия Олеговна","phoneNumber":"+7 (900) 000 0001","email":"eprosvirina#baccasoft.ru","id":58,"parent":47,"cardType":0,"childrens":null}]}]},{"type":"group","level":0,"expand":"-closed","selected":false,"text":"Игнатьева Женевьева Павловна","phoneNumber":"+7 (777) 777 7777","email":"igp#sks.ru","id":3,"parent":null,"cardType":0,"childrens":[{"type":"group","level":1,"expand":"-closed","selected":false,"text":"Меретин Викентий Васильевич","phoneNumber":"+7 (917) 193 5222","email":"keshman#gmail.com","id":2,"parent":3,"cardType":1,"childrens":[{"type":"manager","level":2,"expand":"-empty","selected":false,"text":"Климаков Алексей Александрович","phoneNumber":"+7 (903) 888 8888","email":"krenog#gmail.com","id":20,"parent":2,"cardType":1,"childrens":null}]}]}],
filterKeys = ["text", "phoneNumber", "email"];
function findItemNested(array, searchString, nestingKey) {
return array.reduce((acc, { [nestingKey]: nested, ...o }) => {
if (filterKeys.some(k => o[k] && o[k].includes(searchString)))
acc.push(o)
if (nested)
acc.push(...findItemNested(nested, searchString, nestingKey))
return acc;
}, [])
}
console.log(findItemNested(input, "keshman", "childrens"))
console.log(findItemNested(input, "#gmail.com", "childrens"))
Always Array.reduce returns a single value after processing each element in the array. If you want to return matching records in array then you can use, Array.filter,
[1,2,3,4,5].filter((element) => {
return (element === 2 || element ===4);
});
For this above code, the filtered array will be,
[2,4]
And before filter you should either flat the array or should traverse each and every element to filter the array instead of using Array.filter()
Without reduce also its possible,
var s = [{
"type": "group",
"level": 0,
"expand": "-closed",
"selected": false,
"text": "Федулов Владислав Владиславович",
"phoneNumber": "+7 (927) 999 9999",
"email": "qweeqwe#mail.ru",
"id": 24,
"parent": null,
"cardType": 0,
"childrens": [
{
"type": "group",
"level": 1,
"expand": "-closed",
"selected": false,
"text": "Ширяев Феликс Богуславович",
"phoneNumber": "+7 (123) 456 7810",
"email": "test#test.ru",
"id": 47,
"parent": 24,
"cardType": 0,
"childrens": [
{
"type": "manager",
"level": 2,
"expand": "-empty",
"selected": false,
"text": "Колесова Анастасия Олеговна",
"phoneNumber": "+7 (900) 000 0001",
"email": "eprosvirina#baccasoft.ru",
"id": 58,
"parent": 47,
"cardType": 0,
"childrens": null
},
{
"type": "group",
"level": 2,
"expand": "-empty",
"selected": false,
"text": "Колесова Анастасия Олеговна",
"phoneNumber": "+7 (900) 000 0001",
"email": "eprosvirina#baccasoft.ru",
"id": 534,
"parent": 47,
"cardType": 0,
"childrens": null
},
{
"type": "manager",
"level": 2,
"expand": "-empty",
"selected": false,
"text": "Колесова Анастасия Олеговна",
"phoneNumber": "+7 (900) 000 0001",
"email": "eprosvirina#baccasoft.ru",
"id": 523,
"parent": 47,
"cardType": 0,
"childrens": null
}
]
}
]
},
{
"type": "manager",
"level": 0,
"expand": "-closed",
"selected": false,
"text": "Игнатьева Женевьева Павловна",
"phoneNumber": "+7 (777) 777 7777",
"email": "igp#sks.ru",
"id": 3,
"parent": null,
"cardType": 0,
"childrens": [
{
"type": "group",
"level": 1,
"expand": "-closed",
"selected": false,
"text": "Меретин Викентий Васильевич",
"phoneNumber": "+7 (917) 193 5222",
"email": "keshman#gmail.com",
"id": 2,
"parent": 3,
"cardType": 1,
"childrens": [
{
"type": "manager",
"level": 2,
"expand": "-empty",
"selected": false,
"text": "Климаков Алексей Александрович",
"phoneNumber": "+7 (903) 888 8888",
"email": "krenog#gmail.com",
"id": 20,
"parent": 2,
"cardType": 1,
"childrens": null
}
]
}
]
}];
function filterRequiredElement(arr, searchString, nestingKey) {
arr.forEach((item, index) => {
if (item.type !== searchString || item.phoneNumber !== searchString || item.email !== searchString) {
arr.splice(index, 1);
}
});
for(let item of arr) {
if (item[nestingKey] !== null) {
filterRequiredElement(item[nestingKey], searchString, nestingKey);
}
}
}
filterRequiredElement(s ,'Климаков Алексей Александрович', 'childrens');
console.log(s);

sort an array of array objects by calculating the sum of key value by pairing it by key

I have an array of objects
[{
"data": [{
"name": "John",
"id": 1,
"total": 5
}, {
"name": "Bob",
"id": 2,
"total": 7
}],
"location": "CA"
}, {
"data": [{
"name": "John",
"id": 1,
"total": 1
}, {
"name": "Matt",
"id": 3,
"total": 9
}],
"location": "NY"
}]
And I am trying to sort it by calculating the sum of "total" value by pairing it key "id" in lodash or javascript. This data is given to the highcharts for stacked column. I tried by applying groupBy, sortBy and all the examples I see are using only array and not array of arrays.
The resulting array I am expecting it to be
[{
"data": [{
"name": "Bob",
"id": 2,
"total": 7
},{
"name": "John",
"id": 1,
"total": 5
}],
"location": "CA"
}, {
"data": [{
"name": "Matt",
"id": 3,
"total": 9
},{
"name": "John",
"id": 1,
"total": 1
}, ],
"location": "NY"
}]
One way to tackle this would be using vanilla Javascript:
const arr = [{
"data": [{
"name": "John",
"id": 1,
"total": 5
}, {
"name": "Bob",
"id": 2,
"total": 7
}],
"location": "CA"
}, {
"data": [{
"name": "John",
"id": 1,
"total": 1
}, {
"name": "Matt",
"id": 3,
"total": 9
}],
"location": "NY"
}];
arr.forEach(obj => {
obj.data.sort((a,b) => b.total + b.id - a.total - a.id);
});
console.log(arr);

Filter Array of user objects by array of roles in Javascript

I have an array of user objects.
I want to filter them based on the array of user roles.
filter = ['ROLE_SELLER', 'ROLE_BANK', 'ROLE_CPF', 'ROLE_SLA', 'ROLE_LDAU']
const users = [{
"id": 1,
"email": "user1#test.com",
"name": "User1",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
},
{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}
]
}, {
"id": 2,
"email": "user2#test.com",
"name": "User2",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
}]
}, {
"id": 3,
"email": "user3#test.com",
"name": "User3",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
}]
}, {
"id": 4,
"email": "user4#test.com",
"name": "User4",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
},
{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}
]
}, {
"id": 5,
"email": "user5#test.com",
"name": "User5",
"roles": [{
"id": 5,
"code": "ROLE_LAWYER",
"name": "Lawyer"
}]
}, {
"id": 6,
"email": "user6#test.com",
"name": "User6",
"roles": [{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}]
},
{
"id": 7,
"email": "user7#test.com",
"name": "User7",
"roles": [{
"id": 9,
"code": "ROLE_SLA",
"name": "sla"
}]
},
{
"id": 8,
"email": "user8#test.com",
"name": "User8",
"roles": [{
"id": 8,
"code": "ROLE_BANK",
"name": "Bank"
}]
},
{
"id": 9,
"email": "user9#test.com",
"name": "User9",
"roles": [{
"id": 7,
"code": "ROLE_CPF",
"name": "CPF"
}]
}
]
const filter = ['ROLE_SELLER', 'ROLE_BANK', 'ROLE_CPF', 'ROLE_SLA', 'ROLE_LDAU']
const filteredUsers = users.filter(user => !user.roles.find(role => filter.includes(role.id)))
console.log(filteredUsers)
Expected result
[{
"id": 1,
"email": "user1#test.com",
"name": "User1",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
},
{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}
]
}, {
"id": 4,
"email": "user4#test.com",
"name": "User4",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
},
{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}
]
}, {
"id": 6,
"email": "user6#test.com",
"name": "User6",
"roles": [{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}]
},
{
"id": 7,
"email": "user7#test.com",
"name": "User7",
"roles": [{
"id": 9,
"code": "ROLE_SLA",
"name": "sla"
}]
},
{
"id": 8,
"email": "user8#test.com",
"name": "User8",
"roles": [{
"id": 8,
"code": "ROLE_BANK",
"name": "Bank"
}]
},
{
"id": 9,
"email": "user9#test.com",
"name": "User9",
"roles": [{
"id": 7,
"code": "ROLE_CPF",
"name": "CPF"
}]
}
]
I am trying to image what result you want to receive....
You want to filter of users by array with possible roles, another worlds if user has one of roles of filter array you want to pass his to 'filteredUsers' array?
filter.includes(role.id) - I guess it is wrong, may be you want to
filter by role.code?
Array.find() doesn't support Explorer
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
Array.includes() - doesn't support Explorer too.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
I think the reason why that was happening was that you are trying to match the role id with the 'filter' array which contains the role code. From what i observed from the expected answer, you are looking for user who has any one of their role fits the filter. So do the following will filter by role code and with the expected result
const filteredUsers = users.filter((user) => {
return user.roles.map(role=>filter.includes(role.code)).includes(true)
})
this line of code basically map the filter to every role object to every user, if their role code is included in the filter it will add a true to the array(return array of map()) and for the filter function if the map() return array contains true then true(so basically a || for the whole array)

Filter search based on array values in angular 4

I am new in angular 4. While trying to make a search bar got stuck in the logic. So I have a JSON file, and I am trying to search relevant key value pair of that JSON file based on an array values.
My json file :
const trips = {
"20180201": [{
"journeyId": 1001,
"Number": "001",
"DriverName": "Alex",
"Transporter": {
"id": "T1",
"number": "AN01001",
"Company": "Tranzient"
},
"place": [{
"id": 001,
"value": "Washington DC"
},
{
"id": 002,
"value": "Canberra"
}
],
},
{
"journeyId": 1002,
"Number": "001",
"DriverName": "Tom",
"Transporter": {
"id": "T2",
"number": "AN01002",
"Company": "Trax"
},
"place": [{
"id": 2,
"value": "Canberra"
},
{
"id": 4,
"value": "Vienna"
}
],
},
{
"journeyId": 1003,
"Number": "004",
"DriverName": "Jack",
"Transporter": {
"id": "T3",
"number": "AN01003",
"Company": "Trax"
},
"place": [{
"id": 1,
"value": "Washington DC",
}, {
"id": 4,
"value": "Vienna",
}],
}
],
"20180211": [{
"journeyId": 1004,
"Number": "005",
"DriverName": "Jack",
"Transporter": {
"id": "T3",
"number": "AN01013",
"Company": "Trax"
},
"place": [{
"id": 5,
"value": "Bridgetown"
},
{
"id": 6,
"value": "Ottawa"
},
{
"id": 4,
"value": "Boston"
}
],
},
{
"journeyId": 1005,
"Number": "005",
"DriverName": "Jerry",
"Transporter": {
"id": "T3",
"number": "AN01020",
"Company": "Trax"
},
"place": [{
"id": 5,
"value": "Bridgetown"
},
{
"id": 6,
"value": "Ottawa"
}
],
}
],
"20180301": [{
"journeyId": 1006,
"Number": "005",
"DriverName": "demy",
"Transporter": {
"id": "T3",
"number": "AN01003",
"Company": "Trax"
},
"place": [{
"id": 5,
"value": "Bridgetown"
},
{
"id": 6,
"value": "Vienna"
}
],
}],
};
I have an array
SelectedValues= ["20180201","Vienna"]
All the array values should search depending on the result from its previous array value. For example in this array above, I need to return key value pair of all the places which have its value "Vienna" in date "20180201".
expected output:
trips= {
"20180201": [
{
"journeyId": 1002,
"Number": "001",
"DriverName": "Tom",
"Transporter": {
"id": "T2",
"number": "AN01002",
"Company": "Trax"
},
"place": [{
"id": 2,
"value": "Canberra"
},
{
"id": 4,
"value": "Vienna"
}
],
},
{
"journeyId": 1003,
"Number": "004",
"DriverName": "Jack",
"Transporter": {
"id": "T3",
"number": "AN01003",
"Company": "Trax"
},
"place": [{
"id": 1,
"value": "Washington DC",
}, {
"id": 4,
"value": "Vienna",
}],
}
]
}
So the result returns all the key value pair that have place "Vienna" within the date " 20180201 " .
My solution that I tried to use :
filter(trips, SelectedValues) {
const check = v => options.includes(v) || v && typeof v === 'object' && Object.keys(v).some(l => check(v[l]));
var result = {};
Object.keys(object).forEach(function (k) {
var temp = options.includes(k)
? object[k]
: object[k].filter(check);
if (temp.length) {
result[k] = temp;
}
});
return result;
}
The problem is that it is returning all the key-value pairs that matches all the values in SelectedValues(Implementing OR logic). But I need to implement the array search with AND logic.

Using angularjs forEach loops

I am getting this type of json in my $scope of angularjs:
$scope.someStuff = {
"id": 2,
"service": "bike",
"min": "22",
"per": "100",
"tax": "1",
"categoryservices": [
{
"id": 32,
"category": {
"id": 1,
"name": "software"
}
},
{
"id": 33,
"category": {
"id": 2,
"name": "hardware"
}
},
{
"id": 34,
"category": {
"id": 3,
"name": "waterwash"
}
}
]
}
I want to use angularjs forEach loop and i want to get only category name,
My expected output:
[{"name":"software"}, {"name":"hardware"}, {"name":"waterwash"}]
You can use Array.map()
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
$scope.someStuff.categoryservices.map((x) => { return { name: x.category.name}})
var obj = {
"id": 2,
"service": "bike",
"min": "22",
"per": "100",
"tax": "1",
"categoryservices": [{
"id": 32,
"category": {
"id": 1,
"name": "software"
}
},
{
"id": 33,
"category": {
"id": 2,
"name": "hardware"
}
},
{
"id": 34,
"category": {
"id": 3,
"name": "waterwash"
}
}
]
};
console.log(obj.categoryservices.map((x) => {
return {
name: x.category.name
}
}))
You can use map method by passing a callback function as parameter.
const someStuff = { "id": 2, "service": "bike", "min": "22", "per": "100", "tax": "1", "categoryservices": [ { "id": 32, "category": { "id": 1, "name": "software" } }, { "id": 33, "category": { "id": 2, "name": "hardware" } }, { "id": 34, "category": { "id": 3, "name": "waterwash" } } ] }
let array = someStuff.categoryservices.map(function({category}){
return {'name' : category.name}
});
console.log(array);

Categories

Resources