将地图函数中的数字和 return 编辑后的对象数组相加
sum a number inside a map function and return the edited array of objects
我想通过 ID 找到 属性,如果我找到它,我想将 + 添加到数量 属性。
这是我尝试过但无法实现return具有新编辑数量的先前对象。
有什么想法吗?
const object = [
{
_id: '6078db5aa82f5c34409d53f4',
productId: '60709d8f24a9615d9cff2b75',
quantity: 1,
createdAt: '2021-04-16T00:33:46.816Z',
updatedAt: '2021-04-16T00:33:46.816Z'
},
{
_id: '6078db5aa82f5c34409d53f4',
productId: '60709d8f24a9615d9cff2b76',
quantity: 1,
createdAt: '2021-04-16T00:33:46.816Z',
updatedAt: '2021-04-16T00:33:46.816Z'
},
{
_id: '6078db5aa82f5c34409d53f4',
productId: '60709d8f24a9615d9cff2b77',
quantity: 1,
createdAt: '2021-04-16T00:33:46.816Z',
updatedAt: '2021-04-16T00:33:46.816Z'
}
]
function findID(arr, val ){
return arr.map(function(arrVal){
if( val === arrVal.productId){
return [...arr, {arrVal.quantity +1 }]
}
})
}
findID(object, '60709d8f24a9615d9cff2b77')
在这种情况下,我想 return:
const object = [
{
_id: '6078db5aa82f5c34409d53f4',
productId: '60709d8f24a9615d9cff2b75',
quantity: 1,
createdAt: '2021-04-16T00:33:46.816Z',
updatedAt: '2021-04-16T00:33:46.816Z'
},
{
_id: '6078db5aa82f5c34409d53f4',
productId: '60709d8f24a9615d9cff2b76',
quantity: 1,
createdAt: '2021-04-16T00:33:46.816Z',
updatedAt: '2021-04-16T00:33:46.816Z'
},
{
_id: '6078db5aa82f5c34409d53f4',
productId: '60709d8f24a9615d9cff2b77',
quantity: 2,
createdAt: '2021-04-16T00:33:46.816Z',
updatedAt: '2021-04-16T00:33:46.816Z'
}
]
(object.find((v)=>v.productId==='60709d8f24a9615d9cff2b77') || {}).quantity++;
您可以使用此代码
此功能应该可以准确地满足您的需求。该函数找到对象,将 1
添加到数量,然后 return 根据您的规范更新对象数组。
const object = [{
_id: '6078db5aa82f5c34409d53f4',
productId: '60709d8f24a9615d9cff2b75',
quantity: 1,
createdAt: '2021-04-16T00:33:46.816Z',
updatedAt: '2021-04-16T00:33:46.816Z'
},
{
_id: '6078db5aa82f5c34409d53f4',
productId: '60709d8f24a9615d9cff2b76',
quantity: 1,
createdAt: '2021-04-16T00:33:46.816Z',
updatedAt: '2021-04-16T00:33:46.816Z'
},
{
_id: '6078db5aa82f5c34409d53f4',
productId: '60709d8f24a9615d9cff2b77',
quantity: 1,
createdAt: '2021-04-16T00:33:46.816Z',
updatedAt: '2021-04-16T00:33:46.816Z'
}
];
const findID = (arr, id) => (arr.find(product => product.productId === id && ++product.quantity), arr);
console.log(findID(object, '60709d8f24a9615d9cff2b77'));
这里要考虑的另一种可能情况是 return 如果未找到对象,则 false
之类的东西,而不是按原样 return 对象数组。您甚至可以添加第三个参数来有条件地支持该选项。
我想通过 ID 找到 属性,如果我找到它,我想将 + 添加到数量 属性。
这是我尝试过但无法实现return具有新编辑数量的先前对象。
有什么想法吗?
const object = [
{
_id: '6078db5aa82f5c34409d53f4',
productId: '60709d8f24a9615d9cff2b75',
quantity: 1,
createdAt: '2021-04-16T00:33:46.816Z',
updatedAt: '2021-04-16T00:33:46.816Z'
},
{
_id: '6078db5aa82f5c34409d53f4',
productId: '60709d8f24a9615d9cff2b76',
quantity: 1,
createdAt: '2021-04-16T00:33:46.816Z',
updatedAt: '2021-04-16T00:33:46.816Z'
},
{
_id: '6078db5aa82f5c34409d53f4',
productId: '60709d8f24a9615d9cff2b77',
quantity: 1,
createdAt: '2021-04-16T00:33:46.816Z',
updatedAt: '2021-04-16T00:33:46.816Z'
}
]
function findID(arr, val ){
return arr.map(function(arrVal){
if( val === arrVal.productId){
return [...arr, {arrVal.quantity +1 }]
}
})
}
findID(object, '60709d8f24a9615d9cff2b77')
在这种情况下,我想 return:
const object = [
{
_id: '6078db5aa82f5c34409d53f4',
productId: '60709d8f24a9615d9cff2b75',
quantity: 1,
createdAt: '2021-04-16T00:33:46.816Z',
updatedAt: '2021-04-16T00:33:46.816Z'
},
{
_id: '6078db5aa82f5c34409d53f4',
productId: '60709d8f24a9615d9cff2b76',
quantity: 1,
createdAt: '2021-04-16T00:33:46.816Z',
updatedAt: '2021-04-16T00:33:46.816Z'
},
{
_id: '6078db5aa82f5c34409d53f4',
productId: '60709d8f24a9615d9cff2b77',
quantity: 2,
createdAt: '2021-04-16T00:33:46.816Z',
updatedAt: '2021-04-16T00:33:46.816Z'
}
]
(object.find((v)=>v.productId==='60709d8f24a9615d9cff2b77') || {}).quantity++;
您可以使用此代码
此功能应该可以准确地满足您的需求。该函数找到对象,将 1
添加到数量,然后 return 根据您的规范更新对象数组。
const object = [{
_id: '6078db5aa82f5c34409d53f4',
productId: '60709d8f24a9615d9cff2b75',
quantity: 1,
createdAt: '2021-04-16T00:33:46.816Z',
updatedAt: '2021-04-16T00:33:46.816Z'
},
{
_id: '6078db5aa82f5c34409d53f4',
productId: '60709d8f24a9615d9cff2b76',
quantity: 1,
createdAt: '2021-04-16T00:33:46.816Z',
updatedAt: '2021-04-16T00:33:46.816Z'
},
{
_id: '6078db5aa82f5c34409d53f4',
productId: '60709d8f24a9615d9cff2b77',
quantity: 1,
createdAt: '2021-04-16T00:33:46.816Z',
updatedAt: '2021-04-16T00:33:46.816Z'
}
];
const findID = (arr, id) => (arr.find(product => product.productId === id && ++product.quantity), arr);
console.log(findID(object, '60709d8f24a9615d9cff2b77'));
这里要考虑的另一种可能情况是 return 如果未找到对象,则 false
之类的东西,而不是按原样 return 对象数组。您甚至可以添加第三个参数来有条件地支持该选项。