我有对象arrlist
和参数uid
的数组
如果属性value
有重复的值(忽略null),并且id
与uid
不相同,则自动递增该值,直到没有重复的值。
如何使用JavaScript实现上述场景。试过了但卡住了
var uid1 = 26
var arrlist1 = [
{id: 11 , value: 2},
{id: 14, value: null},
{id: 28, value: 1},
{id: 26, value: 3},
{id: 78, value: 3}
]
Expected Output
[
{id: 11 , value: 2},
{id: 14, value: null},
{id: 28, value: 1},
{id: 26, value: 3},
{id: 78, value: 4}
]
In the above scenario,
{id: 26, value: 3} is not incremented since id matches with uid
for `id` 78, `value` 3 is duplicate, so increment it
{id: 78, value: 4}
and no more duplicate return expected output.
var uid2 = 78
var arrlist2 = [
{id: 11 , value: 2},
{id: 14, value: null},
{id: 28, value: 1},
{id: 26, value: 3},
{id: 78, value: 1}
]
Expected Output
[
{id: 11 , value: 3},
{id: 14, value: null},
{id: 28, value: 2},
{id: 26, value: 4},
{id: 78, value: 1}
]
In the above scenario,
{id: 78, value: 1} is not incremented since id matches with uid
for `id` 28, `value` 1 is duplicate, so increment it becomes
{id: 28, value: 2}
for `id` 11 `value` 2 is duplicate, so increment it becomes
{id: 11, value: 3}
for `id` 26 `value` 3 is duplicate, so increment it becomes
{id: 26, value: 4}
and no more duplicate return expected output.
Tried
const result = this.getArrObj(arrlist1, uid1)
function getArrObj (arrlist, uid) {
const totalobj = arrlist.filter(e=>e!==null).length);
var finalArrObj = arrlist.map(itm => ({
...itm,
value: (itm.value && (itm.id !== uid && itm.value <= totalobj)) ?
(itm.value || 0) + 1 : itm.value
})
}
1条答案
按热度按时间pgx2nnw81#
解决方案2