Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

80
Views
List count array by name attribute dynamics information

I need to show an object with the existing attribute list and showing a number of how many times it appears:

Payload:

[
    [
        "valor"
    ],
    [
        "tipo de entrada",
        "data entrada",
        "valor"
    ],
    [
        "tipo de entrada",
        "data entrada",
        "valor"
    ],
    [
        "tipo de entrada"
    ],
    [
        "tipo de entrada"
    ],
    [
        "tipo de entrada"
    ],
    [
        "valor"
    ],
    [
        "valor"
    ],
    [
        "valor"
    ],
    [
        "valor"
    ],
    [
        "tipo de entrada",
        "data entrada",
        "valor"
    ]
]

Result:

{
    valor: 16,
    tipo_entrada: 9,
    dqta_entrada: 6
}

being that list of attributes dynamically, it can contain other values ​​(value, name, age ...)

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can flatten your array using array#flat() and then using array#reduce count the frequency of each word.

const arr = [ [ "valor" ], [ "tipo de entrada", "data entrada", "valor" ], [ "tipo de entrada", "data entrada", "valor" ], [ "tipo de entrada" ], [ "tipo de entrada" ], [ "tipo de entrada" ], [ "valor" ], [ "valor" ], [ "valor" ], [ "valor" ], [ "tipo de entrada", "data entrada", "valor" ] ],
      result = arr.flat().reduce((r, word) => {
        r[word] = (r[word] ?? 0) + 1;
        return r;
      },{});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

Here's a one-liner :

var mainData = [ [ "valor" ], [ "tipo de entrada", "data entrada", "valor" ], [ "tipo de entrada", "data entrada", "valor" ], [ "tipo de entrada" ], [ "tipo de entrada" ], [ "tipo de entrada" ], [ "valor" ], [ "valor" ], [ "valor" ], [ "valor" ], [ "tipo de entrada", "data entrada", "valor" ] ]

var resultObj = {};
mainData.map(data => data.forEach(subData => { resultObj[subData] = (Object.keys(resultObj).indexOf(subData) == -1 ? 1 : resultObj[subData] + 1) }))
console.log(resultObj)

about 4 years ago · Juan Pablo Isaza Report

0

You can use flat and play around with it.

This code defines and explain a way of resolving your problem:

const result = {};
const initialArray = [['tomato'], ['ketchup', 'potato']];
const strings = initialArray.flat(); // ['tomato', 'ketchup', 'potato']

for (const string of strings) {
  // string is 'tomato' for example
  // result[string] would then be result[tomato]

  if (result[string]) {
    result[string] += 1; // result.tomato already exists? Add 1 to it
  } else {
    result[string] = 1; // if it does not exists, create it with value 1
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!