javascript - 如何在数组中执行更新功能?
问题描述
我有 updateFruit 功能。用户可以在其中更新他们的水果选择。我从 API 获得了水果的详细信息。只是为了展示我这样展示的概念fruit_temp
。里面的所有值fruit_temp
都是通过 API 获取的,这意味着水果已经存在。现在我想执行更新功能。
在此更新功能中,用户可以添加新水果或从选项中删除他们选择的水果。如果将新水果Mango
添加到 中updateFruit_temp
,它将像这样发送数据
{
"fruit_db_id": null,
"fruit_id": 4,
"status": true,
}
如果用户想Papaya
从水果选择中删除示例,它将发送这样的数据
{
"fruit_db_id": 71,
"fruit_id": 1,
"status": false,
}
但是现在我在更新水果并将数据发送到上面的预期输出时遇到了问题。如何updateFruit_temp
使用 API 中请求的格式推送新的更新值?我必须像这样将数据发送到 API。
//if new fruit is inserted
{
"fruit_db_id": null,
"fruit_id": 4,
"status": true,
},
//if fruit is removed
{
"fruit_db_id": 71,
"fruit_id": 1,
"status": false,
}
//if user wants to keep the fruit
{
"fruit_db_id": 71,
"fruit_id": 1,
"status": true,
}
以下是我尝试过的,但updateFruit_temp
没有回应newFruit
如何解决这个问题的新值?
let fruit_temp = [];
let updateFruit_temp = [];
let status = true;
$(document).ready(function() {
getFruitDetails();
//updateFruit();
});
const getFruitDetails = () => {
//getDetail through API and pass the selected value into the fruit temp
fruit_temp = [{
db_id: 71,
id: 1,
name: 'papaya'
}, {
db_id: 73,
id: 3,
name: 'apple'
}];
//console.log(fruit_temp);
}
const updateFruit = () => {
let newFruit = $('#updateFruit').val();
fruit_temp = fruit_temp.filter(function(el) {
if (el != null) {
updateFruit_temp.push({
"fruit_db_id": (el.db_id == undefined) ? null : el.db_id,
"fruit_id": (el.id == undefined) ? newFruit : el.id,
"status": (el.db_id == undefined || el.db_id == null) ? false : true,
}) - 1;
return el;
}
});
console.log(newFruit);
console.log(updateFruit_temp);
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/css/bootstrap-select.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/js/bootstrap-select.min.js"></script>
<label> Add new fruit :</label>
<select class="selectpicker" multiple data-live-search="true" id="updateFruit">
<option value="1" selected>Papaya</option>
<option value="2">Honeydew</option>
<option value="3" selected>Apple</option>
<option value="4">Mango</option>
</select>
<button type="submit" onclick="updateFruit()"> Update Fruit Choice</button>
这不是错误,它只是没有执行更新功能。意思是我updateFruit_temp
的不工作。
updateFruit_temp
如果用户从选择中选择水果,这就是我要实现的目标honeydew, apple and removed papaya
。
{
"fruit_db_id": null,
"fruit_id": 4,
"status": true, // add honeydew
},
{
"fruit_db_id": 73,
"fruit_id": 3,
"status": true, //maintain apple
},
{
"fruit_db_id": 71,
"fruit_id": 1,
"status": false, //remove papaya
}
解决方案
推荐阅读
- python-3.x - 如何在不实际写入队列的情况下测试写入队列
- google-analytics - 如何为支付推荐网站设置谷歌分析/谷歌标签管理器
- python - 尽管没有更改,JSON Web 令牌不再具有“解码”属性
- python - 如何在 Pyspark Dataframe 的特定索引中添加行或替换?
- javascript - 身份服务器:在 Angular 中登录和注销后重定向?
- database - Terraform for Digital Ocean 托管数据库防火墙
- actions-on-google - Google Action 让麦克风在没有提示的情况下为用户命令打开
- vba - 在 VBA 中使用“And _”避免嵌套 IF
- azure - 如何在 Azure Active Directory 中设置服务原则显示名称
- reactjs - GraphQL / Relay - 是否需要在主 QueryRenderer 中查询所有表?