How to find how many time a same number comes in a array

Hello my json is

{"test1":[{ "user1"},{"user2"},{"user1"}]}

I need to find how many user1 is available in test1

First you have a syntax error in your json

It should be like this

{"test1": ["user1", "user2", "user3"]}

If you want to list any user inside test1 you can use JSON.parse() function to make the data like a javascript object and then use the array.forEach() method to check the elements inside the array.

let json = '{"test1": ["user1", "user2", "user3"]}';

let data = JSON.parse(json);

data.test1.forEach(element => {
	console.log(element);
})

Response

user1
user2
user3

lol but any way my main json

{"votes":[
{"id":"586214600412758040",
"discriminator":"5859",
"username":"SudhanPlayz",
"timestamp":1585625602413},
{"id":"289463060178010123",
"discriminator":"0001",
"username":"Alex Op",
"timestamp":1585655106484}
]
}

How to find Same user ID Occurs in above json

Sorry I read again your question. If you want to know how many user are available in test1 use array.length property

let json = '{"test1": ["user1", "user2", "user3"]}';

let data = JSON.parse(json);

console.log(data.test1.length); // Gives you 3

In your case with the votes

let json = '{"votes":[{"id":"586214600412758040","discriminator":"5859","username":"SudhanPlayz","timestamp":1585625602413},{"id":"289463060178010123","discriminator":"0001","username":"Alex Op","timestamp":1585655106484}]}';

let data = JSON.parse(json);

console.log(data.votes.length); // Gives you the total of votes

data.votes.forEach(element => {
	console.log(element.id); // Gives you the id of each user
});
1 Like

ok @ElBort i got 2nd code but how to find SAME ID Occurs i mean if there is same like 2 votes how to find that is 2

@ElBort

Discord:
Me: /myvotes
Bot: takes message author id and checks how many time id occurs and want to reply that

Maybe there is prettier ways to do this… But this should work

let json = '{"votes":[{"id":"586214600412758040","discriminator":"5859","username":"SudhanPlayz","timestamp":1585625602413},{"id":"289463060178010123","discriminator":"0001","username":"Alex Op","timestamp":1585655106484}]}';

let data = JSON.parse(json);

console.log('total votes', data.votes.length);

let count = 0;
let id = 586214600412758040;

data.votes.forEach(element => {
	if(element.id == id) count ++;
});

console.log('votes from id', count)
1 Like

I will try and after make your post as Solution

@ElBort

var json, vote1;
  var v1 = await fetch(
    "https://api.botlist.space/v1/bots/689664350037868568/upvotes",
    {
      method: "GET",
      headers: {
        authorization: process.env.BOTLISTSPACE
      }
    }
  ).then(async res => {
    json = await res.json();
    
    let count = 0;
    let id = message.author.id;

    json.user.forEach(element => {
	if(element.id == id) count ++;
});
    
    vote1 = `
[BotList.Space](https://botlist.space/bot/689664350037868568/upvote) - ${count}
`;
  }); 

RESPONCE

[ { user:
     { id: '586214600412758040',
       username: 'SudhanPlayz',
       discriminator: '5859',
       avatar: '65f206f3524c20941c4bca4dffa79c3e',
       short_description: 'Developer of Alexa | Visit: https://alexa-discord.tk/' },
    timestamp: 1585135224508 },
  { user:
     { id: '634701839983706112',
       username: 'khalby786',
       discriminator: '1531',
       avatar: '9a67d6b0212c11fe1aefd3c8b41bb07f',
       short_description: '' },
    timestamp: 1585138569038 },
  { user:
     { id: '592792499739820230',
       username: 'Unique/Dev',
       discriminator: '0005',
       avatar: 'a_3480e96bc7835250a6b47a013172703b',
       short_description: '' },
    timestamp: 1585196167785 },
  { user:
     { id: '461633827157311490',
       username: 'an0Kx42',
       discriminator: '9294',
       avatar: 'b7c6e196554af6cd9c25c07d344a6dc8',
       short_description: '' },
    timestamp: 1585208302337 },
  { user:
     { id: '586214600412758040',
       username: 'SudhanPlayz',
       discriminator: '5859',
       avatar: '65f206f3524c20941c4bca4dffa79c3e',
       short_description: 'Developer of Alexa | Visit: https://alexa-discord.tk/' },
    timestamp: 1585287003011 },
  { user:
     { id: '637961685851832340',
       username: 'МishаG4mеr',
       discriminator: '0001',
       avatar: 'a_6108ecad0082dbd48020c4b5feb925b5',
       short_description: '' },
    timestamp: 1585330127234 },
  { user:
     { id: '586214600412758040',
       username: 'SudhanPlayz',
       discriminator: '5859',
       avatar: '65f206f3524c20941c4bca4dffa79c3e',
       short_description: 'Developer of Alexa | Visit: https://alexa-discord.tk/' },
    timestamp: 1585373883008 },
  { user:
     { id: '685189595473772554',
       username: 'JAYishh',
       discriminator: '0005',
       avatar: 'a_872a1f363590e63f3f0df2eafee0fb13',
       short_description: '' },
    timestamp: 1585387244126 },
  { user:
     { id: '667247393271578650',
       username: '𝘾𝝰𝙥𝙧𝖎𝙘𝞂𝙣ヅ',
       discriminator: '7777',
       avatar: '04032cb629de9c7ddfcd46d31a20d55b',
       short_description: '' },
    timestamp: 1585405367229 },
  { user:
     { id: '589364609711079434',
       username: 'ZeroTwo',
       discriminator: '0200',
       avatar: 'e7c4999ba676fac973734a5e657994a8',
       short_description: '' },
    timestamp: 1585405393786 },
  { user:
     { id: '595609854463442947',
       username: '高梨 力香',
       discriminator: '5406',
       avatar: 'cef774475a5e66f725bf32e994cd7b1c',
       short_description: '' },
    timestamp: 1585405417375 },
  { user:
     { id: '327797154116534272',
       username: 'Aresgamming',
       discriminator: '3490',
       avatar: '140997d1c466d64cdb22e6f7570d1fe3',
       short_description: '' },
    timestamp: 1585405482919 },
  { user:
     { id: '665202985189769227',
       username: '!   🥀ʜʏᴜɴᴀ🥀   !',
       discriminator: '5653',
       avatar: '73e8ad62862b791790d2bd39187004d0',
       short_description: '' },
    timestamp: 1585405497062 },
  { user:
     { id: '586214600412758040',
       username: 'SudhanPlayz',
       discriminator: '5859',
       avatar: '65f206f3524c20941c4bca4dffa79c3e',
       short_description: 'Developer of Alexa | Visit: https://alexa-discord.tk/' },
    timestamp: 1585475813737 },
  { user:
     { id: '685189595473772554',
       username: 'JAYishh',
       discriminator: '0005',
       avatar: 'a_872a1f363590e63f3f0df2eafee0fb13',
       short_description: '' },
    timestamp: 1585477534901 },
  { user:
     { id: '324507072270172160',
       username: 'Cc99X_YT',
       discriminator: '7825',
       avatar: '1eadee2afef4ffa05196190b73bc096d',
       short_description: '' },
    timestamp: 1585480959467 },
  { user:
     { id: '395501972926758912',
       username: '"  ⎲ÁkaCrafter CZ',
       discriminator: '3111',
       avatar: 'a_a1e60a4ab6eb96767abaaef667d20a01',
       short_description: '' },
    timestamp: 1585483129039 },
  { user:
     { id: '670760943227240478',
       username: 'CoolGamerYT',
       discriminator: '4108',
       avatar: '9d21ed96e36ed4f3e2d2bd0a06990cb2',
       short_description: '' },
    timestamp: 1585489379537 },
  { user:
     { id: '586214600412758040',
       username: 'SudhanPlayz',
       discriminator: '5859',
       avatar: '65f206f3524c20941c4bca4dffa79c3e',
       short_description: 'Developer of Alexa | Visit: https://alexa-discord.tk/' },
    timestamp: 1585563892626 },
  { user:
     { id: '685189595473772554',
       username: 'JAYishh',
       discriminator: '0005',
       avatar: 'a_872a1f363590e63f3f0df2eafee0fb13',
       short_description: '' },
    timestamp: 1585576723923 },
  { user:
     { id: '637961685851832340',
       username: 'МishаG4mеr',
       discriminator: '0001',
       avatar: 'a_6108ecad0082dbd48020c4b5feb925b5',
       short_description: '' },
    timestamp: 1585578021818 },
  { user:
     { id: '586214600412758040',
       username: 'SudhanPlayz',
       discriminator: '5859',
       avatar: '65f206f3524c20941c4bca4dffa79c3e',
       short_description: 'Developer of Alexa | Visit: https://alexa-discord.tk/' },
    timestamp: 1585650920758 },
  { user:
     { id: '289463060178010123',
       username: 'Alex Op',
       discriminator: '0001',
       avatar: 'a70fcac392675f04fabf3d9bc7d33f26',
       short_description: '' },
    timestamp: 1585655091086 } ]

This is a new code here .forEach undefined

This is because you forget to check the json tree form.

element.id doesn’t exist in the response you sent.

It should be element.user.id.

@ElBort I tried not working. if you code please give me code

Your error is here

json.user.forEach(element => {
	if(element.id == id) count ++;
}

It should look for every array

json.forEach(element => { // here iterate all elements inside json
	if(element.user.id == id) count ++; // then check if the id is equal
}

Anyway, do not ask people to code for you. You should learn to code this by yourself. I was just helping you to find the answer to your question

2 Likes