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    
大佬们可以看下嘛
阅读 3.1k
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

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)
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)
推荐问题