Home > Software engineering >  Can I get all elements of an array as separate strings?
Can I get all elements of an array as separate strings?

Time:08-28

Like the title says, I want to get every element of an array as its own string in order to check if a certain other array includes any of the strings from array 1.

I feel like I've been trying at this forever and I'm not sure if it's even possible but I could just be stupid. Any help would be greatly appreciated!

const v = items.map((value) => {
        if(value.tags.includes('weapon')) return value.id
})
const results = v.filter(e => {
    return e !== undefined
})
const c = Object.keys((challengerItems)).map((key) => {return key})
const o = Object.keys((opposerItems)).map((key) => {return key})

const va = results.filter(element => {
    return element
});

if(challengerItems === undefined) return message.reply(`\`❓\` <@${message.author.id}> does not have any weapons! Duel canceled!`)
if(opposerItems === undefined) return message.reply(`\`❓\` <@${opposer.id}> does not have any weapons! Duel canceled!`)

if(!c.includes(CHECK IF USER HAS ANY WEAPONS)) return message.reply(`\`❓\` <@${message.author.id}> does not have any weapons! Duel canceled!`) 
if(!o.includes(CHECK IF OTHER USER HAS ANY WEAPONS)) return message.reply(`\`❓\` <@${opposer.id}> does not have any weapons! Duel canceled!`) 

CodePudding user response:

Use map to convert every element to string (I used toString() one could JSON.stringify if that suits)

Use find and indexOf to locate items from array 2 that are included in array 1

var arr1 = [2, 17, 'B'];

var arr2 = ["18", "17", "dog"];

// convert arr1 elements to string
arr1 = arr1.map(item => item.toString())

console.log(arr2.find(item => arr1.indexOf(item) > -1))

  • Related