1

I have an array of thousands objects and need to check if a specific value is included in one of the values.

My array:

const images = [
  {
    "name" : "sky",
    "width" : "90",
    "height" : "150",
    "colours" : ["pink", "yellow", "red"],
  },
  {
    "name" : "old car",
    "width" : "90",
    "height" : "150",
    "colours" : ["dark purple", "sand", "light green"],
  },
  {
    "name" : "sky",
    "width" : "90",
    "height" : "150",
    "colours" : ["dark blue", "violet"],
  },
...
]

and I'm trying to get a new array that contains only those objects which include a specific colour. Also the value must be a perfect match: i.e. if I pass "blue" it should return false if it checks against "dark blue"

I'm trying to do the following but running into errors

const checkColourtHandler = async (selectedColour) => {
   const imageList = images.filter(function (item, i) {
        if (item.colours.includes(selectedColour)) {
          return true;
        } else return false;
   });
   console.log(imageList);
}

Ideally I'm looking for the best performant method as well.

3 Answers 3

1

Use the filter and includes methods to check for matching elements:

const images = [
  {
    "name" : "sky",
    "width" : "90",
    "height" : "150",
    "colours" : ["pink", "yellow", "red"],
  },
  {
    "name" : "old car",
    "width" : "90",
    "height" : "150",
    "colours" : ["dark purple", "sand", "light green"],
  },
  {
    "name" : "sky",
    "width" : "90",
    "height" : "150",
    "colours" : ["dark blue", "violet"],
  }
];

const filtered = (col) => images.filter(i => i.colours.includes(col));

const output = filtered('dark blue');

console.log(output);
console.log(filtered('blue'));

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

8 Comments

Thanks @DigitalDrifter it looks like it is working however it returns a function. In reality I'm trying to get the array as final result so I can save it and use it elsewhere.
Where is it returning a function? I'm just defining filtered as a function and calling it with desired color string: filtered('dark blue');. That can change to suit your implementation needs, of course.
Well I need to get and save the array result of that function somewhere. So the way I see it is that there are 2 steps: run the function and save the result in other variable so I can access it and use it elsewhere.
I updated the answer. You'll see const output = filtered('dark blue'); now so you can reference the output array later in your code.
That is not working for me for some reason. I have const output = filtered('dark blue'); within another function and filtered() separate and I'm getting Unhandled promise rejection: TypeError: undefined is not an object (evaluating 'i.colours.includes'
|
0

This should do it

const images = [
  {
    "name" : "sky",
    "width" : "90",
    "height" : "150",
    "colours" : ["pink", "yellow", "red"],
  },
  {
    "name" : "old car",
    "width" : "90",
    "height" : "150",
    "colours" : ["dark purple", "sand", "light green"],
  },
  {
    "name" : "sky",
    "width" : "90",
    "height" : "150",
    "colours" : ["dark blue", "violet"],
  }
];

const checkColourtHandler = (selectedColour) => images.filter(item => item.colours.includes(selectedColour));

console.log(checkColourtHandler("sand"));
console.log(checkColourtHandler("violet"));

Comments

0

@DigitalDrifter

Updating my code here

const images = [
  {
    "name" : "sky",
    "width" : "90",
    "height" : "150",
    "colours" : ["pink", "yellow", "red"],
  },
  {
    "name" : "old car",
    "width" : "90",
    "height" : "150",
    "colours" : ["dark purple", "sand", "light green"],
  },
  {
    "name" : "sky",
    "width" : "90",
    "height" : "150",
    "colours" : ["dark blue", "violet"],
  }
];

const filtered = (col) => images.filter(i => i.colours.includes(col));

const checkColourtHandler = (selectedColour) => {
    const result = filtered(selectedColour); 
    console.log("result: ",result);
  }

checkColourtHandler('pink');

1 Comment

Find the problem, another error in my code was causing a conflict. Many thanks for your help!

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.