상세 컨텐츠

본문 제목

forEach() - advanced

JavaScript

by 폴리프레임 2024. 7. 5. 14:51

본문

반응형

const output =  [
  {
    score: 0.9977799654006958,
    label: 'remote',
    box: { xmin: 30, ymin: 68, xmax: 189, ymax: 117 }
  },
  {
    score: 0.9982636570930481,
    label: 'remote',
    box: { xmin: 328, ymin: 66, xmax: 370, ymax: 195 }
  },
  {
    score: 0.9958969950675964,
    label: 'couch',
    box: { xmin: -3, ymin: 0, xmax: 636, ymax: 472 }
  },
  {
    score: 0.999091625213623,
    label: 'cat',
    box: { xmin: 4, ymin: 53, xmax: 323, ymax: 469 }
  },
  {
    score: 0.998751699924469,
    label: 'cat',
    box: { xmin: 330, ymin: 20, xmax: 648, ymax: 370 }
  }
]

const countObj = {}

output.forEach(({ score, label })=> {
    if (score > 0.9) {
        if (countObj[label]) {
            countObj[label]++;
        } else {
            countObj[label] = 1;
        }
    }
})

console.log(countObj) // {remote: 2, couch: 1, cat: 2}

'JavaScript' 카테고리의 다른 글

scripts 유닉스 -> 윈도우  (0) 2024.07.16
json() 과 JSON.parse()  (2) 2024.07.05
JSON.stringify()  (0) 2024.07.05
clsx, twMerge, cn, shadcn-ui  (0) 2024.07.05
toJSON(), JSON.stringify()  (1) 2024.06.18

관련글 더보기