如何在nodejs中发送api数据例如作为描述框给出
Posted
技术标签:
【中文标题】如何在nodejs中发送api数据例如作为描述框给出【英文标题】:how to send api data in nodejs For Example given as the description box 【发布时间】:2022-01-20 08:06:30 【问题描述】:假设我有一个对象数组:
它将多个值存储在一个变量中。该对象可以包含用户名、金额等。 实际上,我将数据的相同对象存储在单个对象中,就像描述中的定义一样。 所以,帮助我每个人都可以解决这个问题。 请解决这个问题。
[
"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
【问题讨论】:
如果您分享一些示例并尝试一些东西会很好。并在 js fiddle 中分享 【参考方案1】:不确定我的理解是否正确,但您只是想为具有特定用户名的用户获取正确的信息?
如果是这样,那么您可以在您的第一个对象上使用 .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
【讨论】:
非常感谢先生以上是关于如何在nodejs中发送api数据例如作为描述框给出的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Sequelize/Node JS 中为数据类型 TIME 发送数据
Laravel REST API 类型保留(例如,如何防止整数作为字符串发送)
使用nodejs客户端库将图像作为base64编码发送到google云视觉API