Javascript 尝试将对象放入数组时,此筛选函数不会删除旧对象
回购协议: 我的邮递员收藏: 预期: 当前的“狮子”字符:Javascript 尝试将对象放入数组时,此筛选函数不会删除旧对象,javascript,arrays,node.js,express,put,Javascript,Arrays,Node.js,Express,Put,回购协议: 我的邮递员收藏: 预期: 当前的“狮子”字符: [ { "id": "1", "name": "Teemo", "pride": "LoL", "age": "1", "gender": "male" }, { "id": "2", "name": "Nasus", "pride": "LoL", "age": "10
[
{
"id": "1",
"name": "Teemo",
"pride": "LoL",
"age": "1",
"gender": "male"
},
{
"id": "2",
"name": "Nasus",
"pride": "LoL",
"age": "10",
"gender": "male"
}
]
放
该机构:
{
"age": "1",
"gender": "female",
"name": "LuLu",
"pride": "LoL"
}
应在“获取所有狮子”时返回此新列表:
[
{
"id": "1",
"name": "LuLu",
"pride": "LoL",
"age": "1",
"gender": "female"
},
{
"id": "2",
"name": "Nasus",
"pride": "LoL",
"age": "10",
"gender": "male"
}
]
结果
PUT函数
app.put()
方法可能存在的一个潜在问题是,当您执行const oldLion=lions.filter((lion=>lion.id==paramId))
时,您将得到一个数组。查看更多关于此的信息。因此,我相信您希望使用而不是过滤器()
,因为稍后您将调用:
const newLion = Object.assign(updated, oldLion);
并且,如果oldLion
是一个数组
,数值属性
将添加到更新的
对象中,如下一个示例所示:
const updated={somekey:“somevalue”};
log(Object.assign(已更新,[{somekey:“updatedValue”}])代码>
.as控制台{背景色:黑色!重要;颜色:石灰;}
.as console wrapper{max height:100%!important;top:0;}
在app.put()
上,当您执行const oldLion=lions.filter((lion=>lion.id==paramId))
时,您将得到一个数组作为结果。我相信您希望在那里使用而不是筛选器,因为稍后您会调用它:const newLion=Object.assign(updated,oldLion)
。正是这样const oldLion=lions.find((lion=>lion.id==paramId))
,我还需要更新我的Object.assignconst newLion=Object.assign({id:oldLion.id},updated)
想发布答案吗?当然,我很快会给出答案。。。
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const path = require('path')
const port = 3000
app.use(express.static('client'))
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
let lions = []
let id = 0
app.get('/lions', function(req, res) {
console.log('GET lions:', lions)
res.json(lions)
})
app.get('/lions/:id', function(req, res) {
let lion = lions.filter((lion => lion.id === req.params.id))
res.json(lion || {})
})
app.post('/lions', function(req, res) {
id++
const lion = Object.assign({ id: id.toString() }, req.body)
lions.push(lion)
res.json(lion)
});
app.put('/lions/:id', function(req, res) {
const paramId = req.params.id
const updated = req.body
if (updated.id) {
delete updated.id
}
const oldLion = lions.filter((lion => lion.id === paramId))
if (!oldLion) {
res.send()
}
const newLion = Object.assign(updated, oldLion)
console.log('newLion', newLion)
lions = lions.filter(lion => lion.id !== paramId)
lions.push(newLion)
res.json(newLion)
});
app.listen(port, () => console.log(`NODE RUNNING on port: ${port}`))
app.put('/lions/:id', function(req, res) {
const paramId = req.params.id
const updated = req.body
if (updated.id) {
delete updated.id
}
// Here I find the oldLion to replace by id:
const oldLion = lions.filter((lion => lion.id === paramId))
if (!oldLion) {
res.send()
}
// Here I create a new object for the new "lion":
const newLion = Object.assign(updated, oldLion)
console.log('newLion', newLion)
// Here I filter out the old lion:
lions = lions.filter(lion => lion.id !== paramId)
// New lion is pushed in:
lions.push(newLion)
res.json(newLion)
});
const newLion = Object.assign(updated, oldLion);