我正在尝试从费用数组中提取date
,expenseInfo
,category
和amount
。但是我得到expenses.map() is not a function
。
下面是代码。
我要去哪里错了?
let expenses = [{
"user": {
"id": "5bab847a5b0d2e2ce8b4cbe5",
"name": "test_user",
"email": "test@gmail.com",
"username": "test"
},
"date": "07-Sep-2018",
"expenseInfo": "Starbuck",
"category": "Restaurants",
"amount": 15
},
{
"user": {
"id": "5bab847a5b0d2e2ce8b4cbe5",
"name": "test_user",
"email": "test@gmail.com",
"username": "test"
},
"date": "01-Aug-2018",
"expenseInfo": "Shopping",
"category": "Michael Kors bag",
"amount": 90
}
];
let expensesData = [];
let expenses1 = [];
expensesData.push(expenses);
expenses1 = expensesData.map((expense, index) => {
`<tr>
<td>${expense[index].date}</td>
<td>${expense[index].expenseInfo}</td>
<td>${expense[index].category}</td>
<td>${expense[index].amount}</td>
</tr>`
})
答案 0 :(得分:1)
当您将费用记入“费用数据”时,您会在0th index
处获得费用。您需要避免使用push
或尝试下面的代码
let expenses = [{
"user": {
"id": "5bab847a5b0d2e2ce8b4cbe5",
"name": "test_user",
"email": "test@gmail.com",
"username": "test"
},
"date": "07-Sep-2018",
"expenseInfo": "Starbuck",
"category": "Restaurants",
"amount": 15
},
{
"user": {
"id": "5bab847a5b0d2e2ce8b4cbe5",
"name": "test_user",
"email": "test@gmail.com",
"username": "test"
},
"date": "01-Aug-2018",
"expenseInfo": "Shopping",
"category": "Michael Kors bag",
"amount": 90
}
];
let expensesData = [];
let expenses1 = [];
expensesData.push(expenses);
expenses1 = expensesData[0].map((expense, index) => {
`<tr>
<td>${expense.date}</td>
<td>${expense.expenseInfo}</td>
<td>${expense.category}</td>
<td>${expense.amount}</td>
</tr>`
})
答案 1 :(得分:0)
let expenses = [{
"user": {
"id": "5bab847a5b0d2e2ce8b4cbe5",
"name": "test_user",
"email": "test@gmail.com",
"username": "test"
},
"date": "07-Sep-2018",
"expenseInfo": "Starbuck",
"category": "Restaurants",
"amount": 15
},
{
"user": {
"id": "5bab847a5b0d2e2ce8b4cbe5",
"name": "test_user",
"email": "test@gmail.com",
"username": "test"
},
"date": "01-Aug-2018",
"expenseInfo": "Shopping",
"category": "Michael Kors bag",
"amount": 90
}
];
let expenses1 = [];
expenses1 = expenses.map((expense) => {
return `<tr>
<td>${expense.date}</td>
<td>${expense.expenseInfo}</td>
<td>${expense.category}</td>
<td>${expense.amount}</td>
</tr>`
})
console.log(expenses1)
答案 2 :(得分:0)
我认为您正在尝试这样做。
在Map
中,您仅需要返回一些值,然后expense1
数组将具有值,否则它将用undefined
填充。
let expenses = [{
"user": {
"id": "5bab847a5b0d2e2ce8b4cbe5",
"name": "test_user",
"email": "test@gmail.com",
"username": "test"
},
"date": "07-Sep-2018",
"expenseInfo": "Starbuck",
"category": "Restaurants",
"amount": 15
},
{
"user": {
"id": "5bab847a5b0d2e2ce8b4cbe5",
"name": "test_user",
"email": "test@gmail.com",
"username": "test"
},
"date": "01-Aug-2018",
"expenseInfo": "Shopping",
"category": "Michael Kors bag",
"amount": 90
}
];
let expensesData = [];
let expenses1 = [];
expensesData = [...expenses];
expenses1 = expensesData.map((expense, index) => {
return `<tr>
<td>${expense.date}</td>
<td>${expense.expenseInfo}</td>
<td>${expense.category}</td>
<td>${expense.amount}</td>
</tr>`
})
答案 3 :(得分:0)
我认为您正在尝试这样做。 它对我有用
expenses1 = expensesData[0].map(function(expense, index){return yours statement here});
答案 4 :(得分:0)
从您发表的评论中我看到的是,您得到的费用是JSON字符串而不是数组。您必须做JSON.parse(expenses)
,然后一切都会按预期进行
expenses = JSON.parse(expenses);
let result = expenses.map((expense, index) => {
return `<tr>
<td>${expense[index].date}</td>
<td>${expense[index].expenseInfo}</td>
<td>${expense[index].category}</td>
<td>${expense[index].amount}</td>
</tr>`
});
console.log(result);
您可以从下面的链接检查普通javascript对象和JSON对象之间的区别
What is the difference between JSON and Object Literal Notation?
What are the differences between JSON and JavaScript object? [duplicate]