Let's say I have an array of objects:
It stores multiple values in a single variable. The object can contain like a Username, amount etc. Actually, I store the same object of the data in the single objects like a define in the description. So, help me every one can solve this problem. please solve this problem.
[
{
"id": 2,
"Username": "Rajesh",
"Amount": "3000",
"Account_name": "Rahul",
"Google_pay": "",
"Phone_pay": "9794368090",
"Paytm": "",
"Account_holder_name": "",
"Ifsc": "",
"Date": "2021-12-16T13:47:19.000Z"
},
{
"id": 3,
"Username": "john$123",
"Amount": "3000",
"Account_name": "Rahul",
"Google_pay": "",
"Phone_pay": "9794368090",
"Paytm": "",
"Account_holder_name": "",
"Ifsc": "",
"Date": "2021-12-16T13:47:19.000Z"
},
{
"id": 8,
"Username": "alex",
"Amount": "2000",
"Account_name": "Rahul",
"Google_pay": "",
"Phone_pay": "",
"Paytm": "9794368090",
"Account_holder_name": "",
"Ifsc": "",
"Date": "2021-12-16T13:47:19.000Z"
},
{
"id": 9,
"Username": "rajesh",
"Amount": "1000",
"Account_name": "Rahul",
"Google_pay": "",
"Phone_pay": "9794368090",
"Paytm": "",
"Account_holder_name": "",
"Ifsc": "",
"Date": "2021-12-16T13:47:19.000Z"
},
{
"id": 10,
"Username": "rahul1",
"Amount": "1000",
"Account_name": "Rahul",
"Google_pay": "",
"Phone_pay": "9794368090",
"Paytm": "",
"Account_holder_name": "",
"Ifsc": "",
"Date": "2021-12-16T13:47:19.000Z"
},
{
"id": 11,
"Username": "john$123",
"Amount": "2000",
"Account_name": "xyz",
"Google_pay": "9794368090",
"Phone_pay": "",
"Paytm": "",
"Account_holder_name": "",
"Ifsc": "",
"Date": "2021-12-16T13:51:20.000Z"
},
{
"id": 14,
"Username": "rajesh",
"Amount": "1200",
"Account_name": "asfsddfs",
"Google_pay": "",
"Phone_pay": "",
"Paytm": "8778979",
"Account_holder_name": "",
"Ifsc": "",
"Date": "2021-12-17T08:42:17.000Z"
}
]```
However, I can't seem to get an array that looks like this
```{
"john$123": [
{
"id": 3,
"Username": "john$123",
"Amount": "3000",
"Account_name": "Rahul",
"Google_pay": "",
"Phone_pay": "9794368090",
"Paytm": "",
"Account_holder_name": "",
"Ifsc": "",
"Date": "2021-12-16T13:47:19.000Z"
},
{
"id": 11,
"Username": "john$123",
"Amount": "2000",
"Account_name": "xyz",
"Google_pay": "9794368090",
"Phone_pay": "",
"Paytm": "",
"Account_holder_name": "",
"Ifsc": "",
"Date": "2021-12-16T13:51:20.000Z"
}
],
"alex": [
{
"id": 8,
"Username": "alex",
"Amount": "2000",
"Account_name": "Rahul",
"Google_pay": "",
"Phone_pay": "",
"Paytm": "9794368090",
"Account_holder_name": "",
"Ifsc": "",
"Date": "2021-12-16T13:47:19.000Z"
}
],
"rajesh": [
{
"id": 2,
"Username": "Rajesh",
"Amount": "3000",
"Account_name": "Rahul",
"Google_pay": "",
"Phone_pay": "9794368090",
"Paytm": "",
"Account_holder_name": "",
"Ifsc": "",
"Date": "2021-12-16T13:47:19.000Z"
},
{
"id": 9,
"Username": "rajesh",
"Amount": "1000",
"Account_name": "Rahul",
"Google_pay": "",
"Phone_pay": "9794368090",
"Paytm": "",
"Account_holder_name": "",
"Ifsc": "",
"Date": "2021-12-16T13:47:19.000Z"
},
{
"id": 14,
"Username": "rajesh",
"Amount": "1200",
"Account_name": "asfsddfs",
"Google_pay": "",
"Phone_pay": "",
"Paytm": "8778979",
"Account_holder_name": "",
"Ifsc": "",
"Date": "2021-12-17T08:42:17.000Z"
}
],
"rahul1": [
{
"id": 10,
"Username": "rahul1",
"Amount": "1000",
"Account_name": "Rahul",
"Google_pay": "",
"Phone_pay": "9794368090",
"Paytm": "",
"Account_holder_name": "",
"Ifsc": "",
"Date": "2021-12-16T13:47:19.000Z"
}
]
}``
I have been able to make some sort of headway, but can not quite get the exact result I need. Any guidance is greatly appreciated, thanks!
21
CodePudding user response:
Not sure if I understood you correctly but you just want to get the information for a user with a specific username correct?
If so, then you can use .filter() on your first object, like I have shown below
let array = [] //your original array
let username = "rajesh" //the username whose info you want to get
let userData = array.filter(results => results.Username.toLowerCase() === username.toLowerCase())
console.log(userData) //this will contain all the elements that had their username the same as the username in the username variable (case insensitive)
//do stuff with the new filtered user data
Now if you want the data as json with the key being the username, you can just do this
let returnData = {}
returnData[username] = userData
return returnData