0

I need to get the "grid_id" from the "grids" array ( Step 1 )

Check if this "grid_id" exists in the "variationsDB" array ( Step 2 )

Using this "grid_id", take the "variation_id" and check if it exists in the "variations" array ( Step 3 )

If in any step the return is false, return an error

I can only think of two or more forEach :(

I need something cleaner

Code:

// Step 1
"grids": [
    {
        "grid_id": 1
    },
    {
        "grid_id": 2
    }
],

// Step 2
"variationsDB": [
    {
        "variation_id": 3,
        "grid_id": 1
    },
    {
        "variation_id": 7,
        "grid_id": 2
    }
],

// Step 3
"variations": [
    {
        "variation_id": 3
    },
    {
        "variation_id": 7
    }
]
2
  • can't you reorder the step? so 1. take the grid ids from grids; 2. take variation ids from variations; 3. check if grid id and variation id exists for variationsDB; if yes, you can use Set to make your code cleaner (opinionated of course) Commented Jul 28, 2020 at 3:16
  • With your solution, there would be a considerable amount of combinations between "grid_id" and "variation_id" to later check in the "variationsDB" array. Imagine the "variations" array with dozens of items and the "grid" array with 5 items ... For each item in the "grid" array, you would have to join it with each item in the "variations" array Commented Jul 29, 2020 at 0:05

2 Answers 2

1

Create a function to filter the array. where the first param is the object of an array that exists in the target array you want to filter. second param is the array you would like to filter out the third one is the key please see the code below

// Step 1
const grids = [
  {
    grid_id: 1,
  },
  {
    grid_id: 2,
  },
];

// Step 2
const variationsDB = [
  {
    variation_id: 3,
    grid_id: 1,
  },
  {
    variation_id: 7,
    grid_id: 2,
  },
  {
    variation_id: 8,
    grid_id: 3,
  },
];

// Step 3
const variations = [
  {
    variation_id: 3,
  },
  {
    variation_id: 7,
  },
];


//filter the array for the common value
const filterArray = (a, b, key) => {
  //initialize an array
  const newArr = [];
  a.forEach((x) => {
    b.forEach((y) => {
      if (x[key] === y[key]) {
        //push the item to the initialized
        //if the key of the item is matched
        newArr.push(y);
      }
    });
  });
  //return the array initialized
  return newArr;
};

//assign the filtered array
const firstFilter = filterArray(grids, variationsDB, "grid_id");
const secondFilter = filterArray(firstFilter, variations, "variation_id");

console.log(secondFilter);

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

Comments

0

You could use find and some to check for id in each array

var grids= [ { "grid_id": 1 }, { "grid_id": 2 } ]
var variationsDB=[ { "variation_id": 3, "grid_id": 1 }, { "variation_id": 7, "grid_id": 2 } ]
var variations= [ { "variation_id": 3 }, { "variation_id": 7 } ]

  function checkID(id){
  temp=[]
  ids=grids.find(x => x.grid_id == id)
  if(ids){
  if(variationsDB.some(o => { if(o.grid_id == ids.grid_id ){temp.push(o.variation_id);return true}})){
  if(variations.some(o => o.variation_id == temp[0])) return true 
   else throw new Error(" key not found in variations")
 }
 else throw new Error(" key not found in variationDB")
  }
  else throw new Error(" key not found in grids")
}
console.log(checkID(1))

Comments

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.