0

i want to ask how do we convert the contents of the array into a string and calculate the same value?

ex: 
const arr = [1,1,2,2,3,3,3,3]

If arr contain

values 1 = red,

values 2 = white,

values 3 = brown

**I have tried with the if else method but it doesn't work

my expected output was

red : 2 white : 2 brown : 4

can anyone help me? Thank you in advance

adtybrhn
  • 23
  • 6
  • So you want to count the number of occurrences? If so, see [Counting the occurrences / frequency of array elements](https://stackoverflow.com/questions/5667888/counting-the-occurrences-frequency-of-array-elements). All that is left is setting the correct label – Reyno Dec 02 '21 at 09:03
  • Welcome to Stack Overflow! Visit the [help], take the [tour] to see what and [ask]. If you get stuck, post a [mcve] of your attempt, noting input and expected output using the [`[<>]`](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do) snippet editor. – mplungjan Dec 02 '21 at 09:05
  • No sir, i want to change arr values to string first and count the same value later ex: values 1 is red and it was count = 2 – adtybrhn Dec 02 '21 at 09:07
  • Easier in two steps: `const colors = ["","red","white","brown"]; const count = arr => arr.reduce((prev, curr) => (prev[curr] = ++prev[curr] || 1, prev), {}); const arr = [1,1,2,2,3,3,3,3]; const strings = Object.entries(count(arr)).reduce((acc,[key,val]) => (acc[colors[key]] = val, acc),{}); console.log(strings) ` – mplungjan Dec 02 '21 at 09:24
  • @mplungjan I believe your message is best suited for an answer with code formatting – Apolo Dec 02 '21 at 10:58
  • @Apolo except I closed the question so I would need to post a jsfiddle – mplungjan Dec 02 '21 at 10:59
  • 2
    Oh I missed the fact it was closed, my bad :) – Apolo Dec 02 '21 at 11:01
  • 1. I closed because it is the same - you want a frequency 2. I posted a solution for you too - here is a more readable version: https://jsfiddle.net/mplungjan/tj80nzm7/ – mplungjan Dec 02 '21 at 11:22
  • that's not the method I want, what I want is if array contains values 1 then color is red – adtybrhn Dec 02 '21 at 11:29
  • 1
    That is what my code does. You can even do it in one go: `const colors = ["", "red", "white", "brown"]; const count1 = arr.reduce((acc, curr) => (acc[colors[curr]] = ++acc[colors[curr]], acc), {"red":0, "white":0, "brown":0});` – mplungjan Dec 02 '21 at 11:30
  • Is possible using if else method? If arr contains values 1 is red. because the way you provided it creates a new array. – adtybrhn Dec 02 '21 at 11:32
  • 1
    If you want to replace the array just do so: `const acc = {"red":0, "white":0, "brown":0}; const colorArr = Object.keys(acc); arr = arr.reduce((acc, curr) => (acc[colorArr[curr-1]]++, acc), acc);` https://jsfiddle.net/mplungjan/tj80nzm7/ I added a color replace too `let arr1 = [1, 1, 2, 2, 3, 3, 3, 3]; arr1.forEach((elem,i) => arr1[i] = colors[elem] ); const freq = arr1.reduce((prev, curr) => (prev[curr] = ++prev[curr] || 1, prev), {});` – mplungjan Dec 02 '21 at 11:40
  • I'm sorry I misunderstood your answer sir. your answer is very usefull and thank you in advance – adtybrhn Dec 02 '21 at 14:11
  • @mplungjan how to use divide operation in the code? if red has a value of 2 divided by 2 = 1 – adtybrhn Dec 02 '21 at 14:58
  • When and where and for what reason? – mplungjan Dec 02 '21 at 15:05
  • @mplungjan because the color code is used for the variable socks and if the color is 2 then it is counted as a pair, if 4 it is worth 2 pairs – adtybrhn Dec 02 '21 at 15:10

0 Answers0