Search an array from an array

Hi everyone!!

I am having 2 arrays.
1st array

["happy", "sad"]

2nd array

[{"name": "happy", "value": "1"}, {"name": "sad", "value": "2"}]

What I am trying to do
Search each item in the 1st array to the 2nd array
If the two matches (like happy is there in the two array), I want to get something like this

happy, 1

This is the name and its value from the 2nd array

Is it possible?
Thanks!!!

Hi @ajithkrr :raised_hand_with_fingers_splayed:

Yes you can, first you need specify what exactly do you want to do, if you want only to show the values in array 1 which have matching results in array 2, you can do it like this:

const firstArray = ["happy", "sad"];
const secArray = [{"name": "happy", "value": "1"}, {"name": "sad", "value": "2"}];
let results = []; // This is where we want to save the matching results.

// First, map the second array to its names
let mapped = secArray.map(items => items.name);

// run a forEach loop on the first array
firstArray.forEach(item => {
    let index = mapped.indexOf(item);
    if (index > -1) {
        /* if we found a matching result, we want to save the item and its value
           using the index above, we can access the matching item the second
           array and get its value */
           results.push(secArray[index]);        
    }
})

// After the loop is done, you can run a small code to show you the results
// This is to let you know how many matching results were found.
console.info(`${results.length} items were found!`);

// This is to show you the results, if any
if (results.length > 0) { console.info("The matching results are", results) }

Hope this helps~!
Ahmad

Thanks Ahmad !!
I am trying to get the result from the 2nd array !!!
I want only to show the values in array 2 which have matching results in array 1

@ajithkrr OK, if you follow this example, the console will print the following:

The matching results are
[{name: 'happy', value: '1'}, {name: 'sad', value: '2'}]

Which is the object that has a matching value in the first array. :wink::wink:

If you add other objects into the second array, they will not appear in the printed results.

Thanks
I will try and let you

Thanks @ahmadnasriya !!!
It worked !!

Glad that it did :wink:

in my case I’m getting the error: allcardsInfo.map is not a function

export function uploadDeck_click(event) {
    const { decode } = require('bs58');

    function parseDeckCode(deckCode) {
        const code = deckCode;
        const decoded = decode(code);
        const bytes = Array.from(decoded);
        const ids = [];
        for (let i = 0; i < bytes.length; i += 2) {
            ids.push(bytes[i] + bytes[i + 1] * 256);
        }
        return [ids]; //'1. Original Decklist: ' + deckCode, '2. Code: ' + code, '3. Decoded: ' + decoded, '4. Bytes: ' + bytes, 
    }

    let deckcards = parseDeckCode($w('#inputDeck').value.slice(8));
    //console.log(deckcards)

    getCards().then(allcardsInfo => {
        console.log(allcardsInfo)

        const idCartasDeck = deckcards.toString().split(",");
        console.log(idCartasDeck)

        let results = [] // This is where we want to save the matching results.

        // First, map the second array to its names
        let mapped = allcardsInfo.map((items => items.id))

        // run a forEach loop on the first array
        idCartasDeck.forEach(item => {
            let index = mapped.indexOf(item);
            if (index > -1) {
                /* if we found a matching result, we want to save the item and its value using the index above, we can access the matching item the second array and get its value */
                results.push(allcardsInfo[index]);
            }
        })

        // After the loop is done, you can run a small code to show you the results
        // This is to let you know how many matching results were found.
        console.info(`${results.length} items were found!`);

        // This is to show you the results, if any
        if (results.length > 0) { console.info("The matching results are ", results) }

    })
}

my “getCards” fuction from backend:

import {fetch} from 'wix-fetch';

export async function getCards() {
    const settings = {
        method: 'GET',
        headers: {
            Accept: 'application/json'
        }
    };
    try {
        const fetchResponse = await fetch('https://gist.githubusercontent.com/CristenPerret/5c97ed87212e5385f0034a7999677708/raw/02c3e18aafacedfbb9721161f8b75a7455048e9e/card.json', settings);
        const data = await fetchResponse.json();

        return data;
    } catch (e) {
        return e;
    }
}

my code does retrieve and split the decoding as I want. So I need to check the decoded numbers with the ‘id’ in the ‘card.json’ i get from the backend code. but with your code the only problem is the error “.map” is not a function.

could you help me?!
thanks