0

Given a reference array of individual numbers and paired numbers in nested arrays.

How would you loop over a data set to determine which numbers are part of a pairing and output them to a new array?

// reference array
let refData = [[1,4],3,10,[8,9],6];

// example 1 data to process 
let dataSet = [1, 6, 4, 3];

// expected new outputted array
newData[[1,4],6,3];


// example 2 data to process 
let dataSet = [3, 9, 10, 6, 8];

// expected new outputted array
newData[3,[9,8],10,6];

EDITED:

Thank you for your answers.

Based on the responses I attempted to apply this to my working example, but I'm getting a little stuck. Could you please provide me with a little more help to get me over the hill?

  // Stored reference data
  let invitedGuests = [[9254,9256], 9258, 9261, [9259,9262]];

  // Dynamic received data
  let guestsArrived = [9254, 9258, 9261, 9256];

  let checkedInGuests = []

  for (let i = 0; i < invitedGuests.length; i++) {
    if (Array.isArray(invitedGuests[i])) {
      for (let j = 0; j < invitedGuests[i].length; j++) {
        for (let g = 0; g < guestsArrived.length; g++) {
          if (playerGroups[i][j] == guestsArrived[g]) {
            checkedInGuests[i][j] = guestsArrived[g];
          }
        }
      }
    }

I'm hoping to end up with

 checkedInGuests = [[9254,9256], 9258, 9261]
2
  • Iterate over array. If iterated item is array - iterate over it. Commented Feb 20, 2018 at 17:21
  • 1
    What if dataSet contained only one of the pairs? it contains 8 but not 9? Commented Feb 20, 2018 at 17:22

1 Answer 1

1

Hint: use Array.isArray(item) to check whether an item is single value or array.

refData.forEach(function(item) { 
   if (Array.isArray(item)) {
      checkDataSet(item);
   } else {
      checkItemNormally(item);
   }
});

Example: Below is what I have tested.

var refData = [[1,4],3,10,[8,9],6];
        //var dataSet = [1, 6, 4, 3];
        var dataSet = [3, 9, 10, 6, 8];
        var result = [];

        refData.forEach(function (item) {
            if (Array.isArray(item)) {
                for (var i = 0; i < dataSet.length; i++) {
                    if (item.includes(dataSet[i])) {
                        result.push({
                            key: item,
                            value: item
                        });
                        break;
                    }
                }
            } else {
                // individual
                if (dataSet.includes(item)) {
                    result.push({
                        key: item,
                        value: item
                    });

                }
            }
        });
        result.forEach(function (item) {
            console.log(item.key)
        });

Hope this helps you sorting the problem out.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer! I have tried to use your hint, but am struggling a bit. Would you be able to have a look at my added edited attempt above and let me know where I am going wrong?
@kaiTaylor: the snippets work fine, have you tried it yet?
Thank you, I was able to apply your answer to my issue. It works great! Sorry I wasn't able to get to the finish line on my own from your first hint. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.