假設我有一個物件陣列:
它將多個值存盤在一個變數中。該物件可以包含用戶名、金額等。實際上,我將資料的相同物件存盤在單個物件中,例如描述中的定義。所以,幫我每個人都可以解決這個問題。請解決這個問題。
[
{
"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
uj5u.com熱心網友回復:
不確定我是否理解正確,但您只想獲取具有特定用戶名的用戶的資訊是否正確?
如果是這樣,那么您可以在第一個物件上使用 .filter() ,如下所示
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
現在,如果您希望資料為 json,密鑰為用戶名,您可以這樣做
let returnData = {}
returnData[username] = userData
return returnData
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/383627.html
標籤:javascript 节点.js 数组 目的
