0

I have a target array example: ["Potato", "Orange"] . I'm making an api call that return an object with multiple array which I have to look if the array contain any one of the target array elements.

I have the following Piece of code, but I get following error : Uncaught TypeError: Cannot read property 'includes' of null

I know this is because The object contains some Null values. How Can I fix this?

Example:    const myArray = [
   {
    Track: ["Potato", "Apple"],
           ["Banna"],
           ["Potato", "Batman", "Orange"]
     Type: ["Some", "Stuff"]
     },
   {
    Track: null
     Type: ["Some", "Stuff"]
     }
    ]

const value = "Potato"

const matchingSession = myArray.filter((obj) => value.some(x => { return obj.types.includes(x) || obj.tracks.includes(x)}));

Which it should Return :

[
["Potato", "Apple"],
["Potato", "Batman", "Orange"]
]

This is just an example. It could have more then one object in the array.

3
  • You do not have any properties anywhere named types (nor tracks). Your input also isn't valid Javascript, arrays cannot have key-value pairs Commented Jun 27, 2019 at 21:38
  • 3
    This has nothing to do with promises or Node.js. Unless your question is specifically about those tags, please do not use them. Commented Jun 27, 2019 at 21:38
  • @CertainPerformance Yes You are right sorry for my poorly example. I just edited, close to what I'm currently getting. Thanks Commented Jun 27, 2019 at 21:54

1 Answer 1

3
myArray.filter((arr) => Array.isArray(arr) && arr.length > 0)

This line checks if the array object is a type of array and not empty

Read More about filter

Edit

const arrayOfValues = ["Potato", "Apple"];
myArray.filter((arr) => {

    return Array.isArray(arr) && CheckArray(arrayOfValues, arr) && arr.length > 0
});
function CheckArray(Array, ArrayToCheck) {
    let found = false;
    for (const value of Array) {
        if (ArrayToCheck.includes(value)) {
            found = true;
        }
    }
    return found;
}

Checks if an array includes one of arrayOfValues and if the object is an array and not empty!

Edit 2

const myArray = [
  {
    Track: ["Potato", "Apple"],
    data: [["Banna"],
      ["Potato", "Batman", "Orange"],
      ["Some", "Stuff"]],
  },
  {
    Tack: null,
    data: ["Some", "Stuff"],
  },
];
myArray.map((arr) => {
  return Array.isArray(arr.data) && CheckArray(arr.Track, arr.data);
}).filter(_ => !!_);

function CheckArray(arr, ArrayToCheck) {
  if (!arr || !Array.isArray(arr)) {
    return ArrayToCheck;
  }
  const foundArray = [];    
  if (ArrayToCheck.every(item => Array.isArray(item))) {
    for (const subArr of ArrayToCheck) {
      const ResultArray = CheckArray(arr, subArr);
      if (ResultArray.length > 0) {
        foundArray.push(ResultArray);
      }
    }
  } else {
    if (ArrayToCheck.map(val => arr.includes(val)).includes(true)) {
      foundArray.push(ArrayToCheck);
    }
  }
  return foundArray;
}

Checks if an array includes one of Track data, (works for sub-arrays (2d, etc..))

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

4 Comments

Thanks @JamalAbo I'll try this now! This is Great!
@Mandoxsy, ok, if it meets the requirements, don't forget to upvote and accept the answer!
will this code return a new array with matching element? or just bolean?
@Mandoxsy all codes return matched arrays, (and edit 2 -> for what meets Track requirements)

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.