js如何把数组里相同的值第一个值 置为true

[
    {id:1,value:2},
    {id:2,value:2},
    {id:3,value:1},
    {id:4,value:1},
    {id:5,value:3},
    {id:6,value:4}
]
// TODO 转换成
[
    {id:1,value:2,bol:true},
    {id:2,value:2,bol:false},
    {id:3,value:1,bol:true},
    {id:4,value:1,bol:false},
    {id:5,value:3,bol:true},
    {id:6,value:4,bol:true}
]
根据value值相同的第一个加上一个字段为true其余为false 如果是单个的就直接为true    
大佬们可以看下嘛
已解决 悬赏分:60 - 解决时间 2021-11-27 16:31
反对 0举报 0 收藏 0

回答3

最佳
  • @
    [
        {id:1,value:2},
        {id:2,value:2},
        {id:3,value:1},
        {id:4,value:1},
        {id:5,value:3},
        {id:6,value:4}
    ].map((s => b => {
      const bol = s.has(b.value)
      s.add(b.value)
      return {...b, bol: !bol}
    })(new Set))

    image.png

    支持 0 反对 0 举报
    2021-11-27 09:40
  • @
    let arr = [
        {id:1,value:2},
        {id:2,value:2},
        {id:3,value:1},
        {id:4,value:1},
        {id:5,value:3},
        {id:6,value:4}
    ]
    arr.sort((a, b) => a.value - b.value)
    arr.forEach(item => {
      if (item.value === arr.temp) item.bol = false
      else {
        item.bol = true
        arr.temp = item.value
      }
    })
    console.log(arr)
    const arr = [
      {id:1,value:2},
      {id:2,value:2},
      {id:3,value:1},
      {id:4,value:1},
      {id:5,value:3},
      {id:6,value:4}
    ]
    arr.reduce((acc, item) => {
      const has = acc.includes(item.value)
      item.bol = !has
      !has && acc.push(item.value)
      return acc
    }, [])
    console.log(arr)
    支持 0 反对 0 举报
    2021-11-27 10:37
  • @
    let source = [
        {id:1,value:2},
        {id:2,value:2},
        {id:3,value:1},
        {id:4,value:1},
        {id:5,value:3},
        {id:6,value:4}
    ];
    
    source.forEach((item,index)=>{
        let findIndex = source.findIndex(findItem=>{
            return findItem.value == item.value;
        });
        item.bol = findIndex == index;
    })
    
    console.log(source)
    支持 0 反对 0 举报
    2021-11-27 10:46