Home > other >  How can loop over an array of objects and return true/false when at least one of the object's p
How can loop over an array of objects and return true/false when at least one of the object's p

Time:02-27

i have an array that looks like this

let arrayExample = 
[
 {
 "size":"6M",
 "color":"Blue"
 }
,
 {
 "size":"2M",
 "color":"Yellow"
 }
]

currently, to return all elements of a specific color or a specific size I use this approach

var allColors= arrayExample.map(i=>i.color)

return [{"color":"Blue"}].some((val)=>  allColors.includes(val.color))

my question is how to return a truthy value whenever i found an element with the exact combination i desire

as quoted in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

Return value true if the callback function returns a truthy value for at least one element in the array. Otherwise, false.

my current approach to this is

var allColors= arrayExample.map(i=>i.color)
var allSizes= arrayExample.map(i=>i.size)

return [{"color":"Blue","size":"2M"}].some((val)=>  allColors.includes(val.color) && allSizes.includes(val.size)

this approach doesn't work for my purposes, knowing that even if there is no item in my arrayExample with the combination [{"color":"Blue","size":"2M"}], it would still return true because of the 2M size of the 2nd item of my array

here is a Jsfiddle to test my situation https://jsfiddle.net/8htjpyb9/1/

I would like to thank you if you got this far and also thank you for any and all help, I hope you are having a good day

*edit -> i would like to emphasize that .filter does not work for my problem because of the rest of the code this situation is embedded in.

I am only interested in returning a value of true whenever it finds the exact match.

CodePudding user response:

  • Using Array#map, iterate over the array
  • In every iteration, using Array#some, check if any of the combinations match the current object.
  • To do so, you can use Object#entries and Array#every to check a combination of properties matching the current object.

const array = [ { id: 1, size: '6M', color: 'Blue' }, { ld: 2, size: '2M', color: 'Yellow' }, { id: 3, size: '6M', color: 'Blue' } ];

const combinations = [ { "color": "Blue", "size": "6M" } ];

const matches = array.map(current => 
  combinations.some(combination => 
    Object.entries(combination).every(([key, value]) => 
      current[key] === value
    )
  )
);

console.log(matches);

CodePudding user response:

If you want to return the objects the match filter would be an easier approach. Pass in a set of data, and a test object to a function and return only those object that have the same size and colour values as the test object.

const data=[{id: 1,size:'6M',color:'Blue'},{ld:2,size:'2M',color:'Yellow'},{id:3,size:'6M',color:'Blue'}];

const test = { size: '6M', color: 'Blue' };

function getExact(data, test) {
  return data.filter(obj => {
    return (
      obj.size === test.size
      && obj.color === test.color
    );
  });
}
console.log(getExact(data, test));

If you only want to return true/false if the properties of an array object matches the properties of a test object some is the better option.

const data=[{id: 1,size:'6M',color:'Blue'},{ld:2,size:'2M',color:'Yellow'},{id:3,size:'6M',color:'Blue'}];

const test = { size: '6M', color: 'Blue' };
const test2 = { size: '5M', color: 'Green' };

function getExact(data, test) {
  return data.some(obj => {
    return (
      obj.size === test.size
      && obj.color === test.color
    );
  });
}

console.log(getExact(data, test));
console.log(getExact(data, test2));

CodePudding user response:

If I understand correctly you are looking for this?

const getItem = (arr, toFind) => {
  const {size, color} = toFind;
  return arr.filter(item => item.color === color && item.size === size).length > 0
};
let arrayExample = [{
    "size": "6M",
    "color": "Blue",
    "material": "plastic"
  },
  {
    "size": "2M",
    "color": "Yellow",
    "material": "leather"
  }
]


const res = getItem(arrayExample,{"size": "6M","color": "Blue"})
console.log(res)

  • Related